Search Unity

Custom Preloader Problem

Discussion in 'Web' started by Evgeny-Eliseev, Aug 10, 2016.

  1. Evgeny-Eliseev

    Evgeny-Eliseev

    Joined:
    Jan 21, 2015
    Posts:
    19
    I needed to show progress of loading of engine files (.mem, .js).
    But by default Unity shows progress only for .data file.
    So, I patched Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\UnityConfig\decompress.js.

    But now I need to patch every version of Unity at every developer machine and can't use Unity Cloud Build.

    Maybe there is more beautiful way to do this? Like WebGLTemplate, but for UnityLoader?

    I'm using Unity 5.4.0f3.
     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Hello Evgeny-Eliseev.

    The UnityLoader.js is generated in a very specific way and currently it is not possible to make it a part of the template. Nevertheless, there are other ways to make a necessary adjustment, and you don't need to patch any internal Unity files for this (in fact I can hardly imagine a situation when this would be necessary).

    The most universal way would be to modify the JavaScript code at launch time. Any plugins with .jspre extension are added on the top of the UnityLoader.js and will be executed before any other code (at least starting from 5.4), so you are free to do any adjustments to the code there. Let's consider your loading progress example - in 5.4 all the main downloaded files go through the function LoadCompressedFile(url, onload, onprogress), so the only thing you need to do is to override it.

    Add the following Assets/Plugins/UnityProgress.jspre to your project:
    Code (JavaScript):
    1. var _LoadCompressedFile = LoadCompressedFile;
    2. LoadCompressedFile = function (url, onload, onprogress) {
    3.   return _LoadCompressedFile(url, onload, function (e) {
    4.     var id = url == Module.codeUrl ? 'code' : url == Module.dataUrl ? 'data' : url == Module.memUrl ? 'mem' : 'unknown';
    5.     console.log(id + ' progress: ' + e.loaded / e.total);
    6.     if (onprogress)
    7.       onprogress(e);
    8.   });
    9. };
    Then check the output in the console:
    Screen Shot 2016-08-10 at 17.33.29.png

    Also note that you might need to set Content-Type: application/octet-stream for the .js file to enforce the Content-Length header, as it is required to correctly calculate the progress.
     
    Evgeny-Eliseev likes this.