/// <summary> /// Get the bus attached to a GameObject, or create one. /// </summary> /// <typeparam name="T">The Message type</typeparam> /// <param name="go">Gameobject that the bus lives on.</param> /// <returns>If the object is alive, a valid bus. If the object is dead, a dummy bus.</returns> public static IBus <T> GetBus <T>(GameObject go) { if (go == null) { return(new DummyBusImpl <T>()); } MessageBusBehaviour localBus = go.GetComponent <MessageBusBehaviour>() ?? go.AddComponent <MessageBusBehaviour>(); return(localBus.GetBus <T>()); }
private static void InitGlobalBus() { Assert.IsTrue(Application.isPlaying, "Global MessageBus cannot be used unless the game is running."); if (_globalBusBehaviourInitialized) { return; } _globalBusBehaviourInitialized = true; _globalBusBehaviour = new GameObject("[Global MessageBus]").AddComponent <MessageBusBehaviour>(); UnityEngine.Object.DontDestroyOnLoad(_globalBusBehaviour.gameObject); }