Unity StrangeIoc框架 (三)signal信号方式

先创建TestRoot

 
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中进行之前一样的绑定 。

我们写一个脚本 把所有的信号都放在里面。   在具体情况中可以分模块将信号放在一起

 
  public class StartSignal : Signal
    {
    }
    public class ClickSignal : Signal
    {
    }
    public class RequestSignal : Signal<string>
    { 
   } public class SuccessSignal : Signal {
   }
 

StartCommand与之前的事件方法没有区别

 
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 有略微的不同    直接实例信号进行派发

 
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中进行监听  然后执行方法

 
 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 通过注入获取

在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 中  我们对成功的信号进行派发

 
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();
        }
    }
 

这样  我们就实现了信号的机制  信号也是官方推荐的方法

posted on 2017-05-04 15:43  Sun‘刺眼的博客  阅读(2184)  评论(0编辑  收藏  举报

导航