Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Two unity webgl demos on one html page, or more.

Discussion in 'WebGL' started by pera2, Aug 12, 2016.

  1. pera2

    pera2

    Joined:
    Aug 10, 2016
    Posts:
    14
    Can we have more then one unity canvas on the same html page?
     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Hello pera2.

    If you try to embed multiple WebGL content into the same document, then in addition to multiple canvases, you will also have duplicate global functions and variables (including i.e. Module). Although it is technically possible to workaround this, I would rather recommend to use iframes instead. Then you may embed as many games on a single page as you wish, because all of them will run in their own isolated iframe context (still, you might consider destroying the instance of the previous game as soon as the user launches the next one, otherwise you might run out of memory quite soon in a single-process browser on a 32-bit system)

    P.S. Answering your question from another related thread. Currently we are working on a much more advanced loader that in the future should let you programmatically embed, preload, control and destroy multiple WebGL content on the same page without using iframes.
     
    Last edited: Aug 12, 2016
    pera2 likes this.
  3. pera2

    pera2

    Joined:
    Aug 10, 2016
    Posts:
    14
    how to destroy the instance of unity game and free the memory?
    for example, user has option to close 3D content and continue working on the webpage.
     
  4. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    This is a bit tricky. The problem is that there is no way to synchronously destroy an object in JavaScript. The only way to do this is to destroy all references to the object and wait for it to get garbage collected.
    I just took a deep look at it and it appears that in order to garbage collect the heap, you would have to remove all references to it both from the main code and from inside the asm.js module, which can not be achieved without modifying the asm.js code (unless you compile you build with ALLOW_MEMORY_GROWTH and then use _emscripten_replace_memory to downsize the heap).
    On the other hand, an easy solution would be to just use an iframe for the game and when it is no longer needed, set the iframe src to something else, which should completely destroy all the content of the iframe and free your memory.
     
    pera2 likes this.