Beispiel #1
0
 public void Dispose()
 {
     mPendingCalls = null;
     if (mOutstandingInitCall != null)
     {
         mOutstandingInitCall.Exit();
         mOutstandingInitCall = null;
     }
 }
Beispiel #2
0
        // Make a call to a Javascript function that has been exposed as an interface to Unity.
        // If mAllowedCalls has not yet been populated, the call will be deferred until it is.
        // Throws ArgumentException if we attempt to call a function that Javascript hasn't
        // told us is in our interface.
        private IReceipt MakeJSCall(string func, List <object> argList, Action <string> callbackFunc)
        {
            // If in the editor, we to a basic emulation of what's gonna happen in browser.  Provide a dummy receipt.
            if (Application.isEditor)
            {
                if (callbackFunc != null)
                {
                    IScheduler scheduler = GameFacade.Instance.RetrieveMediator <SchedulerMediator>().Scheduler;
                    scheduler.StartCoroutine(DoCallbackInSeconds(1.0f, callbackFunc));
                }
                return(new JSReceipt(null));
            }

            if (mAllowedCalls.Count == 0)
            {
                JSReceipt earlyJSR = new JSReceipt(null);
                mPendingCalls.Enqueue(delegate()
                {
                    // Fire off the call, but only if the early receipt didn't get killed
                    if (!earlyJSR.hasExited())
                    {
                        JSReceipt realJSR = (JSReceipt)MakeJSCall(func, argList, callbackFunc);
                        earlyJSR.UpdateGO(realJSR.GetGO());
                    }
                });
                return(earlyJSR);
            }

            if (!mAllowedCalls.ContainsKey(func))
            {
                throw new ArgumentException("Non-allowed function call to Javascript: " +
                                            func +
                                            ".  Check JSDispatcher.mAllowedCalls!");
            }

            if (argList == null)
            {
                argList = new List <object>();
            }

            // Null callbackFunc is okay, we just won't do anything on callback.

            string     cbId = "JSCallback" + mCallbackCounter++;
            GameObject go   = new GameObject(cbId);
            JSCallback cb   = (JSCallback)go.AddComponent(typeof(JSCallback));

            cb.Register(callbackFunc);

            argList.Insert(0, cbId);
            argList.Insert(1, "Callback");
            Application.ExternalCall("Hangout.unityDispatcher.unityToJSCalls." + func, argList.ToArray());

            return(new JSReceipt(go));
        }
Beispiel #3
0
        public JSDispatcher()
        {
            // If running in the editor, this class does nothing
            if (Application.isEditor)
            {
                return;
            }

            // If we don't know which calls we're allowed to make, ask the unityDispatcher
            // in Javascript Land.  Asynchronous query, we'll be called back at the
            // delegate we're defining below.  Yay for closure.
            if (mAllowedCalls.Count == 0)
            {
                mOutstandingInitCall = (JSReceipt)MakeForcedJSCall("getExposedFunctions",
                                                                   new List <object>(),
                                                                   delegate(string funcList)
                {
                    try
                    {
                        mAllowedCalls.Clear();
                        string[] funcs = funcList.Split(new Char[] { ',' });

                        foreach (string s in funcs)
                        {
                            if (s != "")
                            {
                                mAllowedCalls.Add(s, true);
                            }
                        }

                        while (mPendingCalls.Count > 0)
                        {
                            Hangout.Shared.Action func = mPendingCalls.Dequeue();
                            func();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("Exception when JSDispatcher received exposed functions: " + ex.Message);
                    }
                });
            }
        }