基于StrangeIOC的HelloWorld的Demo

1、将StrangeIoC框架的包含文件放在游戏工程目录下

 

2、在Game/Scripts/Command下创建StartCommand.cs:

using strange.extensions.command.impl;
using strange.extensions.context.api;
using UnityEngine;
public class StartCommand: EventCommand

{
  public override void Execute()
  {
     Debug.Log("StartCommand Execute!");
  }
}

 

3、在Game/Scripts文件夹下创建一个脚本GameContext继承自MVCSContext,主要用接口绑定、命令绑定、view与mediator绑定:

using UnityEngine;
using System.Collections;
using strange.extensions.context.impl;

using strange.extensions.command.impl;
using strange.extensions.context.api;
public class GameContext: MVCSContext

{
  public GameContext(MonoBehaviour view): base(view)

       {

       }

       protected override void mapBindings()

       {

  base.mapBindings();

  commandBinder.Bind(ContextEvent.START).To<StartCommand>().Once();

  }
}

 

4、在Game/Scripts文件夹下创建一个脚本GameRoot继承自ContextView,这里就是框架程序的入口,把GameRoot拖在unity场景的一个物体上,运行即可执行

using UnityEngine;
using System.Collections;
using strange.extensions.context.impl;
public class DGameRoot : ContextView

{

  void Awake()

  {

  context = new DGameContext(this);

        context .Start();

  }

}

发现控制台打印输出了StartCommand Execute!

 

5、在Game/Server文件夹下创建一个接口IDoSomething,再新建一个接口实现类DoSomething
public interface IDoSomething

{

          void DoSomeFunction();
}

using UnityEngine;
using System.Collections;
public class DoSomething : IDoSomething

{

  public void DoSomeFunction()
  {
  Debug.Log("interface do something function");
  }
}

 

6、在Game/Scripts/Command文件夹下新建一个脚本DoSomethingCommand,用来执行实现的接口功能
using UnityEngine;
using System.Collections;
using strange.extensions.command.impl;
public class DDoSomethingCommand : EventCommand

{
  [Inject]
  public IDoSomething ds { get; set; }
  public override void Execute()
  {
  //执行其脚本
  ds.DoSomeFunction();
  }

}

 

7、这里使用Event,所以需要在某个文件夹下新建一个脚本GameEvent:
public enum DGameEvent

{

 DoSomething,
}

 

8、在GameContext的mapBindings函数中增加以下绑定:

injectionBinder.Bind<IDoSomething>().To<DDoSomething>().ToSingleton();
commandBinder.Bind(DGameEvent.DoSomething).To<DDoSomethingCommand>();

 

9、在StartCommand类的Execute函数中增加测试代码:

 dispatcher.Dispatch(GameEvent.DoSomething);

运行程序会发现控制台打印出:

StartCommand Execute!

interface do something function

 

10.在Game/Scripts/View文件夹下创建一个脚本ButtonView继承自EventView,这个类挂载到场景中的某个按钮中

using UnityEngine;

using UnityEngine.UI;
using System.Collections;
using strange.extensions.mediation.impl;
public class ButtonView : EventView

{
  internal const string CLICK_BUTTON = "CLICK_BUTTON";

        [HideInInspector]

        public Button TestBtn;

        public void Init()

       {

                 TestBtn=transform.Find("TestBtn").GetComponent<Button>();

       }
}

 

11、在Game/Scripts/Mediator文件夹下创建一个脚本ButtonMediator,继承自EventMediator:

using UnityEngine;
using System.Collections;
using strange.extensions.mediation.impl;
public class ButtonMediator :EventMediator

{

  [Inject]

  public ButtonView btnView{get;set;}

  public override void OnRegister()

  {

  btnView.dispatcher.AddListener(ButtonView.CLICK_BUTTON, OnBtnViewClick);

  }

       public override void OnRemove()

  {

  btnView.dispatcher.RemoveAllListeners;

  }


  void OnBtnViewClick()

  {

  Debug.Log("click view button");

  }

}

 

12、不要忘了回到GameContext的mapBindings函数中增加View与Mediator之间的绑定:

mediationBinder.Bind<DButtonView>().To<DButtonMediator>();

 

点击运行,控制台打印出:

StartCommand Execute!

interface do something function

click view button

 

原文链接在这里:

http://www.manew.com/thread-94140-1-1.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值