lessons learned with unity and webgl

30
Lior Tal Unity Developer (Moon Active) http://www.tallior.com Oren “De-Panther” Weizman Unity Developer (Total-Viz) http://de-panther.com/ + = Lessons Learned Getting Wonderball Heroes To WebGL Israeli Unity Developers Meetup 19/4/2015

Upload: lior-tal

Post on 17-Jul-2015

6.574 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Lessons Learned with Unity and WebGL

Lior Tal

Unity Developer (Moon Active)http://www.tallior.com

Oren “De-Panther” Weizman

Unity Developer (Total-Viz)http://de-panther.com/

+ =

Lessons Learned Getting

Wonderball Heroes To WebGLIsraeli Unity Developers Meetup

19/4/2015

Page 2: Lessons Learned with Unity and WebGL

Moon Active

Page 3: Lessons Learned with Unity and WebGL

Wonderball Heroes

2D “Destroy All Pegs” Game

Over 1 Million Downloads

Developed in Unity 4.5.4

Page 4: Lessons Learned with Unity and WebGL

WebGL: JavaScript API for rendering

interactive 2D or 3D graphics inside an HTML5

<canvas> element

WebGWhat ?!

Page 5: Lessons Learned with Unity and WebGL

Why WebGL ?

Chrome (M42) dropped NPAPI support

Page 6: Lessons Learned with Unity and WebGL

WebGL In Unity

- Released in Unity 5 as Preview

- OpenGL ES2

- Officially supports Chrome & Firefox

- Platform Limitations:● Networking

● Audio features

● Script debugging

● Threads

● File I/O

Page 7: Lessons Learned with Unity and WebGL

Unity WebGL Build Process

C#

UnityScript

Boo

Compiled

Code (IL)C++ JavaScript

Mono Compiler IL2CPP Emscripten

Page 8: Lessons Learned with Unity and WebGL

Challenges

New Version

(Unity 5)

New Platform

(WebGL)

New Compiler

(IL2CPP)

Page 9: Lessons Learned with Unity and WebGL

New Version (Unity 5)

- Auto Update - Most of the heavy lifting

- Animator - Changes to “Apply Root Motion”

- AudioSource - new “Spatial Blend”

- Check Plugins for compatibility with Unity 5 !

Page 10: Lessons Learned with Unity and WebGL

New Platform (WebGL)

Encountered 2 WebGL related bugs:- No WWW error reporting

- AudioSource.mute doesn’t work

… Already Fixed in Unity 5.1

Page 11: Lessons Learned with Unity and WebGL

New Compiler (IL2CPP)

- New & Innovative Tech → Bugs

- Issues with JSON.NET

○ Received a “street version” from the publisher ([email protected])

○ No support for generic arrays (Fixed in 5.0.1)

- Check Plugins for compatibility with IL2CPP !!

Page 12: Lessons Learned with Unity and WebGL

Developing for WebGL

- Minimal code changes from WebPlayer → WebGL

- Build Settings

o Optimization Level

- Player Settingso Browser Memory

o Cache

o Enabling Exceptions

o WebGLTemplates

Page 13: Lessons Learned with Unity and WebGL

Developing for WebGL

- Debug HELL (JavaScript Console FTW)

- Compression (gzip)

- Full Screen button - need workarounds

- Mixing domains (CORS)

- New Native Plugin = “.jslib”

Page 14: Lessons Learned with Unity and WebGL

Our Dev Process

- #if (UNITY_WEBPLAYER || UNITY_WEBGL)

- JS Wrapper for FB SDK

- Test as much as possible in editor

Page 15: Lessons Learned with Unity and WebGL

Our Dev Process #2

- Local Web Server (quick updates)

- Redirection to two versionso Chrome → WebGL

o Other Browsers → WebPlayer

Page 16: Lessons Learned with Unity and WebGL

Optimizations

Goal - Minimize Download Size

Initially 300 MB !!- Compression

- Optimization Level

- Exception Support

- Streaming

- Custom Build Script

- Stripping*

Page 17: Lessons Learned with Unity and WebGL

Build Stats

- Editor.log shows stats

after finishing a build

- Assets listed in

decreasing size

Page 18: Lessons Learned with Unity and WebGL

Compression

- Only for non-development builds

- .htaccess on supported servers

- Otherwise, need to manually rename some

stuff

Page 19: Lessons Learned with Unity and WebGL

Optimization Level

- Enables optimizations (Emscripten emcc parameters)

- More optimizations == slower builds

Page 20: Lessons Learned with Unity and WebGL

Exception Support

- Enable / Disable Exception Support

Page 21: Lessons Learned with Unity and WebGL

Build Output

Game data

(all assets, scenes, etc)

Code (converted to js)Binary image for heap

memory (Emscripten)

Embeds the game

content

Supporting files for

initialization

Same as this folder, only

compressed (gzip)

Page 22: Lessons Learned with Unity and WebGL

Streaming In WebGL

- No official solution out of the box, but...

- ...Using an editor script by Unity Developer

(Thanks @jonasechterhoff !)o Shared in the Unity Beta Group

- Implemented as a [PostProcessBuild] step

Page 23: Lessons Learned with Unity and WebGL

In a Streamed Web Player:

“The index of the first level that will have

access to all assets under Resources”

http://docs.unity3d.com/Manual/class-PlayerSettingsWeb.html

First Streamed Level

Page 24: Lessons Learned with Unity and WebGL

Streaming WebGL Solution

1. Split “game.data” and “fileloader.js” to smaller files (one

per scene)

2. Add Resources to .data file of “First Streamed Level”

3. Combine all the new “fileloader” files into one

4. Change the code to run the game after first scene

downloaded

5. Downloads scenes one by one

Page 25: Lessons Learned with Unity and WebGL

Custom Build Script

- Removes unneeded scenes

- Removes resources from the build

- Removes texture usage & downloads them

at runtime

Page 26: Lessons Learned with Unity and WebGL

Final Results

- The game runs smoothly !!- Final build size: 28 MB

- New users - can play after 3 MB

- No need to install any plugin

- Loading time is longer than WebPlayer

Page 27: Lessons Learned with Unity and WebGL

Thank You !

Lior [email protected]

http://www.tallior.com

Oren De-Panther [email protected]

http://de-panther.com/

Page 28: Lessons Learned with Unity and WebGL

Unity Developer

We’re Hiring!

[email protected]

Page 29: Lessons Learned with Unity and WebGL

Tools

● Browser Developer Tools (Chrome, Firefox)

o JS console, Network requests

● Charles (web proxy)

o View network requests + responses

● NGINX - lightweight, portable web server

o Setup a local test environment

Page 30: Lessons Learned with Unity and WebGL

Resources

WebGL

● Owlchemy Labs Port to WebGL - https://hacks.mozilla.org/2014/10/unity-games-in-webgl-

owlchemy-labs-conversion-of-aaaaa-to-asm-js/

● WebGL Streaming - http://forum.unity3d.com/threads/webgl-with-streaming-option-like-

webplayer.316620/

● Fullscreen - http://forum.unity3d.com/threads/webgl-fullscreen-from-within-the-unity-app.318518/

● Emscripten - http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html

● http://forum.unity3d.com/forums/webgl.84/

● http://docs.unity3d.com/Manual/webgl-gettingstarted.html

● WebGL Native Plugin - http://docs.unity3d.com/Manual/webgl-

interactingwithbrowserscripting.html

Unity 5

● Upgrade Guide - http://docs.unity3d.com/Manual/UpgradeGuide5.html