Search Unity

[5.6.0f3] EM autoAddDeps and RefereneError

Discussion in 'Web' started by Chris-HG, Apr 15, 2017.

  1. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Hello.

    Before I submit a bug, I would like to ask if this is a known change from 5.5 to 5.6?
    JSLIB that previously worked in 5.5, now breaks on line 16.
    Was this expected?

    Reference: EM Interacting with code

    Exception:
    Code (JavaScript):
    1. exception thrown: ReferenceError: UNITY3D is not defined,UnityLoader["2258cc28bdc10989ac06d9afc30afa14"]/HGUNITY3DJS.Ready@blob:null/a0f4c54d-5622-ed4f-8b27-dfc125675cad:33:
    JSLIB:
    Code (JavaScript):
    1. console.log("Loading HGUNITY3DJS JSLIB...");
    2.  
    3. var HGUNITY3DJS = {
    4.     //$UNITY3D__deps: [''],
    5.     //$UNITY3D__postset: 'UNITY3D.init()',
    6.     $UNITY3D: {
    7.         init: function() {
    8.             console.log("JSLIB postset called")
    9.         },
    10.         writeToConsole: function(someString) {
    11.             console.log("here is the string from unity " + someString)
    12.         }
    13.     },
    14.     Ready: function(gameObjectName, someString) {
    15.         console.log("entering JSLIB Ready()")
    16.         UNITY3D.writeToConsole(someString) // <=== error here
    17.         console.log("exiting JSLIB Ready()")
    18.     }
    19.     //,$UNITY3D__deps: ['$UNITY3D']
    20. };
    21.  
    22. autoAddDeps(HGUNITY3DJS, '$UNITY3D');
    23. mergeInto(LibraryManager.library, HGUNITY3DJS);
     
  2. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
  3. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Still broken in 5.6.1p1.
     
  4. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    Umm,
    Test your project file in my 5.6.1p1.
    I does not get "UNITY3D is not defined,UnityLoader" exception.
    work in it.

    You need delete HGUNITY3DJS.jslib first line.
    (I think... because, library marge in emscripten context. emscripten context not have console.log())
    and string argument get use Pointer_stringify()
    Code (CSharp):
    1. var HGUNITY3DJS = {
    2.     //$UNITY3D__deps: [''],
    3.     //$UNITY3D__postset: 'UNITY3D.init()',
    4.     $UNITY3D: {
    5.         init: function() {
    6.             console.log("JSLIB postset called")
    7.         },
    8.         writeToConsole: function(someString) {
    9.             someString = Pointer_stringify(someString);
    10.             console.log("here is the string from unity " + someString)
    11.         }
    12.     },
    13.     Ready: function(gameObjectName, someString) {
    14.         console.log("entering JSLIB Ready()")
    15.         UNITY3D.writeToConsole(someString) // <=== error here
    16.         console.log("exiting JSLIB Ready()")
    17.     }
    18.     //,$UNITY3D__deps: ['$UNITY3D']
    19. };
    20.  
    21. autoAddDeps(HGUNITY3DJS, '$UNITY3D');
    22. mergeInto(LibraryManager.library, HGUNITY3DJS);
    5.6.png
     
    Last edited: May 24, 2017
    Chris-HG likes this.
  5. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    ok, weird, when I use "use pre-built engine" then it fails. When unchecked then it works ok.

    How do you view the blob in browser?
     
    Lanre likes this.
  6. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    You can not pass Unity with the blob type.
    So it is necessary to convert from blob type to byte [] type (Uint8Array).
    However, conversion from blob type to Uint8Array type can only be performed asynchronously.
    Call the function by passing a callback function to receive asynchronous results.

    Code (CSharp):
    1. using AOT;
    2. using System;
    3. using System.Runtime.InteropServices;
    4. using UnityEngine;
    5.  
    6. public class test : MonoBehaviour {
    7.     [DllImport("__Internal")]
    8.     private static extern void getBlob(Action<IntPtr, int> callback);
    9.  
    10.     [MonoPInvokeCallback(typeof(Action<IntPtr, int>))]
    11.     private static void resBlob(IntPtr ptr, int length)
    12.     {
    13.         var blobData = new byte[length];
    14.         Marshal.Copy(ptr, blobData, 0, length);
    15.         var tex = new Texture2D(1, 1);
    16.         tex.LoadImage(blobData);
    17.         GameObject.Find("ImagePreivew").GetComponent<Renderer>().material.mainTexture = tex;
    18.     }
    19.  
    20.     void Start () {
    21.         getBlob(resBlob);
    22.     }
    23. }
    Code (JavaScript):
    1. var testPlugin = {
    2.     getBlob: function (resBlob) {
    3.         var ret = blob;
    4.         // Load Image
    5.         fetch('/sample.png').then(function (res) {
    6.             return res.blob();
    7.         }).then(function (blob) {
    8.             // blob => Uint8Array => HEAPU8 => resBlob()
    9.             var fr = new FileReader();
    10.             fr.onload = function (evt) {
    11.                 var ui8a = new Uint8Array(fr.result);
    12.                 var ptr = _malloc(ui8a.byteLength);
    13.                 HEAPU8.set(ui8a, ptr);
    14.                 Runtime.dynCall('vii', resBlob, [ptr, ui8a.length]);
    15.                 _free(ptr);
    16.             }
    17.             fr.readAsArrayBuffer(blob);
    18.         });
    19.     }
    20. };
    21. mergeInto(LibraryManager.library, testPlugin);
     
    Chris-HG likes this.