先建立TestRootapi
using UnityEngine; using System.Collections; using strange.extensions.context.impl; public class TestRoot : ContextView { void Start() { context = new TestContext(this); } }
建立TestContext 在TestContext中咱們須要把事件命令修改成 信號命令網絡
using UnityEngine; using System.Collections; using strange.extensions.context.impl; using strange.extensions.context.api; using strange.extensions.command.api; using strange.extensions.command.impl; public class TestContext : MVCSContext { public TestContext(MonoBehaviour view) : base(view) { } public TestContext(MonoBehaviour view, ContextStartupFlags flags) : base(view, flags) { }
override public IContext Start() { base.Start(); StartSignal startSignal = (StartSignal)injectionBinder.GetInstance<StartSignal>(); startSignal.Dispatch(); return this; }
protected override void addCoreComponents() { base.addCoreComponents(); injectionBinder.Unbind<ICommandBinder>(); injectionBinder.Bind<ICommandBinder>().To<SignalCommandBinder>().ToSingleton(); } protected override void mapBindings() { injectionBinder.Bind<SuccessSignal>().ToSingleton();
injectionBinder.Bind<IService>().To<TestService>();
mediationBinder.Bind<TestView>().To<TestMediator>();
commandBinder.Bind<RequestSignal>().To<RequestCommand>();
commandBinder.Bind<StartSignal>().To<StartCommand>().Once(); } }
strange中IcommandBinder 事件綁定是在addCoreComponents中進行的 因此咱們重寫他 調用它本來的方法 再將ICommandBinder移除,在將他綁定到信號上signalCommandBinder 重寫Start() 而後派發 這樣就完成了 最後在mapBinding中進行以前同樣的綁定 。ide
咱們寫一個腳本 把全部的信號都放在裏面。 在具體狀況中能夠分模塊將信號放在一塊兒this
public class StartSignal : Signal { } public class ClickSignal : Signal { } public class RequestSignal : Signal<string> {
} public class SuccessSignal : Signal {
}
StartCommand與以前的事件方法沒有區別spa
public class StartCommand : Command { [Inject(ContextKeys.CONTEXT_VIEW)] public GameObject contextView { get; set; } public override void Execute() { GameObject test = new GameObject("test"); test.AddComponent<TestView>(); test.transform.SetParent(contextView.transform); } }
TestView 有略微的不一樣 直接實例信號進行派發code
public class TestView : View { public ClickSignal signal_Click = new ClickSignal(); void OnGUI() { if (GUI.Button(new Rect(0, 0, 100, 40), "click")) { signal_Click.Dispatch(); } } }
在TestMediator中進行監聽 而後執行方法orm
public class TestMediator : Mediator { [Inject] public TestView view { get; set; } [Inject] public RequestSignal signal_Request { get; set; } public override void OnRegister() { view.signal_Click.AddListener(OnClick); } public void OnClick() { signal_Request.Dispatch("www.baidu.com"); } }
當被點擊後會進行signal_Request信號的事件派發 RequestSignal 經過注入獲取blog
在RequestCommand中 咱們service 經過注入獲取 而後對他進行了signal_succesd信號的監聽事件
public class RequestCommand : Command { [Inject] public IService service { get; set; } public override void Execute() { Retain(); service.signal_succesd.AddListener(OnComplete); service.Request(); } void OnComplete() { service.signal_succesd.RemoveListener(OnComplete); Debug.Log("get data finish"); Release(); } }
既然添加了監聽 那麼確定有地方會進行成功後的信號派發 在TestService 中 咱們對成功的信號進行派發get
public class TestService : IService { [Inject(ContextKeys.CONTEXT_VIEW)] public GameObject contextView { get; set; } [Inject] public SuccessSignal signal_succesd { get; set; } public void Request() { contextView.GetComponent<MonoBehaviour>().StartCoroutine(Wait()); } /// <summary> /// 模擬網絡請求延遲1秒 /// </summary> /// <returns></returns> IEnumerator Wait() { yield return new WaitForSeconds(1); signal_succesd.Dispatch(); } }
這樣 咱們就實現了信號的機制 信號也是官方推薦的方法