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
原文链接在这里: