Currently we are working on a new embedding scheme for WebGL which should make embedding more convenient, provide namespacing for all the WebGL code in order to avoid interference with the rest of the page, and allow simultaneous embedding of multiple WebGL content on the same page without using iframes.
Instantiation
WebGL content instantiation function is provided with a DOM element (a container for the WebGL content, which can be a usual div and may serve as a placeholder prior to the content instantiation, the element can also be created dynamically), or a DOM element id (this should work even if the element with the provided id has not been parsed yet, i.e. when instantiation is performed in the html header).
After you include UnityLoader.js in your html:<script src="UnityLoaderFolder/UnityLoader.js"></script>
you can use the UnityLoader object to instantiate your game, using for example:var myGame = UnityLoader.instantiate(gameContainer, "http://mydomain/myfolder/Build/mygame.json");
orvar myGame = UnityLoader.instantiate(gameContainer, "http://mydomain/myfolder/Build/mygame.json", {width: 800, height: 600});
orvar myGame = UnityLoader.instantiate("gameContainerId", "http://mydomain/myfolder/Build/mygame.json", {onProgress: myProgress, Module: {TOTAL_MEMORY: 0x20000000}});
UnityLoader.js will not depend on a specific build and generally should be the same for any WebGL build (means it can be shared on a public domain). UnityLoader already contains the default Unity logo and a progress bar, though it can be overridden via additional instantiation parameters. Additional parameters can be also used to override most of the module variables and handlers.
mygame.json will contain all the necessary information to instantiate the build (including parameters and links that have been previously specified in the html). The default module parameters can be overridden from the .json, and the .json parameters can be overridden from the embedding html*.*
The minimal setup should look something like this:
<html>
<head>
<script src="UnityLoader.js"></script>
<script>
var myGame = UnityLoader.instantiate("gameContainerId", "http://mydomain/myfolder/Build/mygame.json");
</script>
</head>
<body>
<div id="gameContainerId" style="width: 960px; height: 600px; margin: auto"></div>
</body>
</html>
All the internal game variables will be wrapped inside the loader functions and therefore will not interfere with the rest of the page or other embedded modules.
The loader will provide the overall build download progress value to the onProgress callback (while the current loader only monitors the download of the .data file). The server should provide Content-Length headers in order for the loading progress to be calculated precisely, otherwise the loading progress will be approximated.
Interaction
Interaction with the instantiated game module is performed through the UnityLoader API, for example:myGame.SetFullscreen(1);
ormyGame.SendMessage("myObject", "myFunction", "foobar");
Distribution
All the WebGL build files (except the UnityLoader.js and .json) will have the same file extension, which should simplify the server setup (i.e. when specifying MIME types for IIS). Compressed and uncompressed content will have the same file extension (no more suffixes required) and will be distributed in the same way, the loader will autodetect the used compression method and automatically perform decompression in JavaScript when necessary. The developer will still be able to provide appropriate Content-Encoding headers in order to speed up decompression of the content. File links will be relative to the .json file location, which should simplify embedding of the WebGL content from another document location or even from another domain (given that CORS headers are provided).
Hash based distribution (optional)
We are also considering a distribution scheme where file names are generated based on the content hashsum.
Consider the following example of the Build folder on the server:
/Build/2e06b6c4670ab40d15e1dcb62436c9b2.json
/Build/c31a82de01db52817dd87d4f1858ee67.json
/Build/57b5a31d2566cf227c47819eb3e5acfa.unityweb
/Build/88a7f58a1038e96586d84b27c3354d5c.unityweb
/Build/8c9889fd3f9272b942d4868a9c1b094c.unityweb
/Build/accdec8ac01fc3e501da10f0fd8cfaec.unityweb
/Build/c05b5450212180ed3d9960c1a050f87d.unityweb
where /Build/2e06b6c4670ab40d15e1dcb62436c9b2.json is:
{
"TOTAL_MEMORY": 268435456,
"dataUrl": "88a7f58a1038e96586d84b27c3354d5c.unityweb",
"codeUrl": "57b5a31d2566cf227c47819eb3e5acfa.unityweb",
"asmUrl": "8c9889fd3f9272b942d4868a9c1b094c.unityweb"
}
In this case, after building a new version of the game, the developer can just copy the new build directly into the same folder on the server, skipping copy of the duplicate filenames. If some files have not changed between builds (for example the updated build has some assets changed but the code remains the same), then all those unchanged build files will remain unchanged on the server and will have the same url, which means they can be served directly from the browser cache, given that the user has already played the previous version of the game. All the previous builds can be simultaneously available from the same server folder, while you will only have to provide the appropriate .json link.
Another reason to use hash based filenames is that this way you can guarantee (up to the degree of the hash collision probability) that urls will not be reused for different content. This is especially important for CDNs and other cached environments, as it guarantees that there will be no version mismatch between the different files in the build (which could happen before if the content from different reused urls got different lifetime in the intermediate caches). This also means that you will be able to set appropriate Cache-Control header for this content to explicitly prevent its revalidation, which should speed up the loading of the cached content.
In addition, we are currently also working on a new caching mechanism for Unity WebGL, which will cache the downloaded content in the indexedDB along with the server response headers. This caching mechanism will be able to emulate the browser cache functionality without having its limitations for the cached content lifetime and size (the Data caching build option will then become deprecated, as you will be able to cache all the build files and not just data, while the versioning will be based on the Last-Modified and/or ETag headers provided by the server). Having hashed filenames will allow developers to avoid content revalidation (particularly important when using a lot of small asset bundles) and cache the content more reliably, even without having any access to the server response headers (i.e. when using public drives and storages).
Feel free to provide any feedback and share additional ideas regarding the future WebGL embedding API.