コード例 #1
0
        private void Awake()
        {
            if (instance == null)
            {
                instance = this;
            }
            else
            {
                throw new Exception("There must only one service at a time");
            }

            SceneManager.sceneLoaded += HandleSceneLoaded;
        }
コード例 #2
0
        private MonoBehaviourService()
        {
            var gameObject = new GameObject {
                hideFlags = HideFlags.HideInHierarchy
            };

            //NOTE When running tests you cannot use DontDestroyOnLoad in editor mode
            if (Application.isPlaying)
            {
                Object.DontDestroyOnLoad(gameObject);
            }
            serviceInstance = gameObject.AddComponent <MonoBehaviourServiceBehaviour>();

            serviceInstance.OnLevelLoaded = () => { if (OnLevelLoaded != null)
                                                    {
                                                        OnLevelLoaded();
                                                    }
            };

            serviceInstance.OnLateUpdate = () => { if (OnLateUpdate != null)
                                                   {
                                                       OnLateUpdate();
                                                   }
            };
            serviceInstance.OnUpdate = () => { if (OnUpdate != null)
                                               {
                                                   OnUpdate();
                                               }
            };
            serviceInstance.OnFixedUpdate = () => { if (OnFixedUpdate != null)
                                                    {
                                                        OnFixedUpdate();
                                                    }
            };

            serviceInstance.ApplicationPause = paused => { if (ApplicationPause != null)
                                                           {
                                                               ApplicationPause(paused);
                                                           }
            };
            serviceInstance.ApplicationFocus = focused => { if (ApplicationFocus != null)
                                                            {
                                                                ApplicationFocus(focused);
                                                            }
            };
            serviceInstance.ApplicationQuit = () => { if (ApplicationQuit != null)
                                                      {
                                                          ApplicationQuit();
                                                      }
            };
        }