Search Unity

How to get a .jslib function to get a value from an AngularJS variable?

Discussion in 'Web' started by LurchUSA, Oct 10, 2018.

  1. LurchUSA

    LurchUSA

    Joined:
    Sep 20, 2017
    Posts:
    21
    .jslib file:

    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.   RunLoadFunction: function () {
    3.     cpa.applicationReady();
    4.   }
    5. });
    C# file that calls the RunLoadFunction:

    Code (CSharp):
    1. public class RunAfterWebGlLoads : MonoBehaviour
    2. {
    3.     private OverWatch _ow;
    4.  
    5.     [DllImport("__Internal")]
    6.     private static extern void RunLoadFunction();
    7.  
    8.     /// <summary>
    9.     /// Starts this instance.
    10.     /// </summary>
    11.     /// <returns></returns>
    12.     public void Start()
    13.     {
    14.         RunLoadFunction();
    15.     }
    16. }
    The AngularJs controller, where the function is I wish to call from the Unity RunAfterWebGlLoads class:

    Code (CSharp):
    1. (function () {
    2.     "use strict";
    3.  
    4.     angular
    5.         .module("controller.photoannotation", [ ])
    6.         .controller("ctrlPhotoAnnotation",
    7.             [
    8.                 "$exceptionHandler",
    9.                 "$rootScope",
    10.                 "$scope",
    11.                 function ($exceptionHandler, $rootScope, $scope) {
    12.                     var cpa = this;
    13.  
    14.                     cpa.gameInstance = UnityLoader.instantiate("gameContainer", "/app/viewmodel/Build/WebGL.json");
    15.  
    16.                     cpa.applicationReady = function() {
    17.                         alert("Ready");
    18.                         SendMessage("Content", "LoadPhoto", "testphoto.png");
    19.                     };
    20.                 }
    21.             ]);
    22. })();
    The result:


    Uncaught ReferenceError: cpa is not defined
    at _RunLoadFunction (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:3521:2)
    at dynCall_v (wasm-function[73075]:12)
    at Object.UnityLoader.52fb4592708d1302df7b4009dd8d002a.Module.dynCall_v (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:26199:36)
    at invoke_v (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:18618:22)
    at _PhotoIo_RunLoadFunction_m2598903460 (wasm-function[67114]:78)
    at dynCall_vii (wasm-function[73101]:17)
    at Object.UnityLoader.52fb4592708d1302df7b4009dd8d002a.Module.dynCall_vii (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:26329:38)
    at invoke_vii (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:18826:24)
    at _PhotoIo_Start_m1457890301 (wasm-function[67115]:226)
    at __Z31RuntimeInvoker_Void_t1185182177PFvvEPK10MethodInfoPvPS4_ (wasm-function[17791]:17)
    at dynCall_iiiii (wasm-function[73000]:20)
    at Object.UnityLoader.52fb4592708d1302df7b4009dd8d002a.Module.dynCall_iiiii (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:25824:40)
    at invoke_iiiii (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:18018:33)
    at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException (wasm-function[69898]:175)
    at _il2cpp_runtime_invoke (wasm-function[70453]:36)
    at __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb (wasm-function[3788]:64)
    at __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb (wasm-function[3786]:253)
    at __ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedE18ScriptingMethodPtr18ScriptingObjectPtrP21ScriptingExceptionPtr (wasm-function[5526]:192)
    at __ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedE18ScriptingMethodPtr18ScriptingObjectPtr (wasm-function[5520]:161)
    at __ZN13MonoBehaviour16DelayedStartCallEP6ObjectPv (wasm-function[8636]:217)
    at __ZN18DelayedCallManager6UpdateEi (wasm-function[6253]:953)
    at __ZZ23InitPlayerLoopCallbacksvEN50EarlyUpdateScriptRunDelayedStartupFrameRegistrator7ForwardEv (wasm-function[9533]:65)
    at __Z17ExecutePlayerLoopP22NativePlayerLoopSystem (wasm-function[8779]:164)
    at __Z17ExecutePlayerLoopP22NativePlayerLoopSystem (wasm-function[8779]:185)
    at __Z10PlayerLoopv (wasm-function[8775]:462)
    at __ZL8MainLoopv (wasm-function[8762]:142)
    at dynCall_v (wasm-function[73075]:12)
    at Object.UnityLoader.52fb4592708d1302df7b4009dd8d002a.Module.dynCall_v (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:26199:36)
    at browserIterationFunc (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:6839:23)
    at Object.runIter (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:6941:5)
    at Browser_mainLoop_runner (blob:http://localhost:62655/3b262011-969c-4e75-bddf-a1f6b6a3973f:6877:20)


    So what am I doing wrong?

    The whole point of this drama is that AFTER the Unity WebGL loads, I need to send a message to my "LoadPhoto" function, which is the file path/name of the photo to load. This value is determined in earlier AngularJS functions, so I have to source this variable from the browser AngularJS itself.

    I have looked into MANY different ways to launch functions/messages to and from WebGL after it loads, but NOTHING is working with most examples given incomplete, confusing our out right wrong.

    So I'm at my wits end and emplore the help of someone to give me a best practices way to send a message into a Unity WebGL instance after it finishes loading.

    Thanks.