WebGL and Unity: Best Practices - Vincent Vergonjeanne

43
WebGL and Unity Best Practices Vincent Vergonjeanne CEO - EVERYDAYiPLAY

Transcript of WebGL and Unity: Best Practices - Vincent Vergonjeanne

Page 1: WebGL and Unity: Best Practices - Vincent Vergonjeanne

WebGL and Unity

Best Practices

Vincent Vergonjeanne

CEO - EVERYDAYiPLAY

Page 2: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Former CEO of Kobojo+50M registered users since 2009

$7.5M Serie A in april 2011Up to 90 employees

Who am I?

Page 3: WebGL and Unity: Best Practices - Vincent Vergonjeanne

CEO of EVERYDAYiPLAYSelf-Funded

24 employees

Who am I?

Page 4: WebGL and Unity: Best Practices - Vincent Vergonjeanne

WHY WEBGL WAS SOIMPORTANT FOR US?

Page 5: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Vikings Gone Wild

• Oct 2013: Vikings Gone Wild on Facebook

– Hybrid Flash/Unity WebPlayer

– 3M registered users

– Launched on iOS, Android 1 year later

Page 6: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Heroes of Paragon

• June 2014: Start of Heroes of Paragon

– Planned to launch with WebPlayer only

– iOS and Android supported from beginning

Page 7: WebGL and Unity: Best Practices - Vincent Vergonjeanne

• April 2015: Chrome kills NAPI

– Strong impact on Vikings Gone Wild

– Heroes of Paragon’s plan for Web are

shutdown

– We become a mobile gaming company

Page 8: WebGL and Unity: Best Practices - Vincent Vergonjeanne

OUR FIRST EXPERIENCEWITH WEBGL

Page 9: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Early days

• Jan 2015: We get access to Unity 5.0 beta

– The game exported properly

– We ran it …

Page 10: WebGL and Unity: Best Practices - Vincent Vergonjeanne

• But early results were disastrous

• 32 bits browser crashes most of the time during JS

parsing phase

• 64 bits browser pass the parsing, but early fps was

terrible (2 fps in average).

• The project.js file for Heroes was 60Mb

uncompressed.

Early days

Page 11: WebGL and Unity: Best Practices - Vincent Vergonjeanne

• April 2015: Another try with official release

• Same results

• We are officially a mobile-only company

Early days

Page 12: WebGL and Unity: Best Practices - Vincent Vergonjeanne

THE BREAKTHROUGH

Page 13: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Breakthrough

• Oct 2015: Facebook team pushes us to re-

explore

– We try again with 5.2.1

– We ran it and WOW!

– Performances for the game was at 30 fps!

– One main issue -> Massive Memory Leak!

Page 14: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Seeking support

• Given early positive result we fully

engaged ourselves to the port and

connected with:

– Facebook dev team

– Mozilla WebGL team

– Unity WebGL team

Page 15: WebGL and Unity: Best Practices - Vincent Vergonjeanne

FROM PROTOTYPE TO RELEASE

Page 16: WebGL and Unity: Best Practices - Vincent Vergonjeanne

DEMO

Page 17: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issues to solve

• Raw Socket

– WebGL doesn’t support Socket and our game

used Socket to connect over TCP-IP to our

server.

– We installed “Sockify” on our server and

adapted client side to it

Page 18: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Raw Sockets

• WebSocket

– Concept it simple -> Wrap your raw socket

around an HTTP call.

GameLogic(TCP)

WebSocket(HTTP)

GameServer (TCP)

WebSockify(HTTP)

Page 19: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Raw Sockets

#if UNITY_WEBGLWebSocket m_socket = null;#elseSocket m_socket = null;#endif

#if UNITY_WEBGLbyte[] tmpBuffer = m_socket.Recv();int count = tmpBuffer.Length;#elsebyte[] tmpBuffer = new byte[m_socket.Available];int count = m_socket.Receive(tmpBuffer, 0, tmpBuffer.Length, SocketFlags.None);#endif

Page 20: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issues to solve

• No Thread Support

– Game logic: used to run on a thread at a tick

of 10 per second.

– We had to profile a couple performance

issues in Path Finding related to this

decoupling.

Page 21: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Threads

• Network Thread

– Chat and Server communication

– Trick is simple:

• Remove blocking Socket read

• Check every update for something to consume on

the WebSocket

Page 22: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Threads

• Bundle Loading Thread

– Simply removing the thread wasn’t enough:

Page 23: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Threads

• Bundle Loading Thread

– Trick:

• Spread each bundles on different frame to let the

browser “breathe”

• Spread custom files parsing of each file on a

different frame. to let the browser “breathe”

Page 24: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issues to solve

• Rendering optimization

– Basic performance was already good.

– But large armies/villages struggled

– On lower PCs, game could drop at 10 fps

Page 25: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Rendering

• Custom Batching

– Dynamic batching wasn’t great for us

– Issues with Shadows, Buildings

– We knew what should be rendered and

when!

Page 26: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Custom Batching

• Solution

– We pre-compute a large mesh with everything static at the moment and re-compute only when needed

– You get the best of both worlds• Equivalent of static batching but with the freedom

to add or remove objects dynamically

• You save all CPU needed to batch at every frames!

Page 27: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Player Preferences

- Player Preferences were not working in

Firefox within an iFrame

- This is going to be fix in FF 43

Page 28: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Player Preferences

• Solution

- We wrote our own abstraction using

localStorage capacity

- Only issue with Chromium or some plugin

limiting access to it

Page 29: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Issue with Music• Music was speeding and slowing down as we were

scrolling.

• We have no music in game today because of it.

• Thought it might be fix today!

Page 30: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Pre-memory allocation

– You need to specify beforehand how much memory your game will need.

– Recommended value is 512Mb

Page 31: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Pre-memory allocation– This value was too short for some of our scenarios.

– We made it 718Mb -> FAIL. We started to have player having this:

The browser could not allocate enough memory for the webgl content

Page 32: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Pre-memory allocation

– The sweet spot for us was 600Mb

– Some 32bits browser might still cap it at 512Mb

Page 33: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Forget about resource folder

– Everything in this folder will be downloaded in

the initial download

– In order to keep our initial download as short as

possible, we removed *everything* from

resource folder and moved them to Bundles

Page 34: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Bundles Download

– Avoid for now WWW.LoadFromCacheOrDownload

– Current implementation uses too much memory and produces lags during bundle decompression

– We did not see this with normal WWW request.

Page 35: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Full Screen Button Issue

– We had a weird bug with Full Screen.

– Don’t trigger it on MouseUp. Unity swallow the

event and doesn’t pass it down to the browser.

– Worked for us on MouseDown.

Page 36: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Issue to solve

• Debugging

– This is a tough one

– At least you have the console that provides some

high level information

– Forget about breakpoint (js file is too big for that)

Page 37: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Debugging

• One ugly way:

– Open JS file with Sublime Text

– Search for the function you want to debug and

add alerts within the function.

– This helped us on really tricky issues.

Page 38: WebGL and Unity: Best Practices - Vincent Vergonjeanne

TO CONCLUDE

Page 39: WebGL and Unity: Best Practices - Vincent Vergonjeanne

First results

• Game was released 3rd of December

– Facebook with WebGL only

– iOS

– Android

Page 40: WebGL and Unity: Best Practices - Vincent Vergonjeanne

First results

• Launch Day Results - Monetization

– WebGL: $0.6 ARPDU

– iOS: $0.44 ARPDU

– Android: $0.26 ARPDU

Page 41: WebGL and Unity: Best Practices - Vincent Vergonjeanne

First results

• First results - Retention

– iOS: 45% D1

– Android: 36% D1

– WebGL: 27% D1

Page 42: WebGL and Unity: Best Practices - Vincent Vergonjeanne

Future

• What’s next?

– Hybrid version WebGL/WebPlayer

– User Acquisition

Page 43: WebGL and Unity: Best Practices - Vincent Vergonjeanne

THANK YOUVincent Vergonjeanne

Twitter: @vvergonEmail: [email protected]