unity有一个叫StrangeIoC的框架插件,这里写了一个使用StrangeIoC的HelloWorld,比他自带的demo更为简单,方便理解
1.插件下载,在Asset Store直接搜索StrangeIoC
2.在Asset文件夹中,新建一个文件夹Game_D,用来放我们编写的脚本,在Game_D文件下创建controller文件夹,用来存放控制脚本,在controller新建一个脚本DGameCommand,继承自EventCommand
using strange.extensions.command.impl; using strange.extensions.context.api; using UnityEngine; public class DGameCommand : EventCommand { public override void Execute() { Debug.Log("Start DGame"); } }
3.在Game_D文件夹下创建一个脚本DGameContext继承自MVCSContext,主要用接口绑定、命令绑定、view绑定
using UnityEngine; using System.Collections; using strange.extensions.context.impl; using strange.extensions.command.impl; using strange.extensions.context.api; public class DGameContext : MVCSContext { public DGameContext(MonoBehaviour view) : base(view) { } protected override void mapBindings() { base.mapBindings(); commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once(); } }
4.在Game_D文件夹下创建一个脚本DGameRoot继承自ContextView,这里就是框架程序的入口,把DGameRoot拖在unity场景的一个物体上,运行即可执行,其实这里就是最小的demo,但是后面还是会写到接口的实现调用,view的实现调用
using UnityEngine; using System.Collections; using strange.extensions.context.impl; public class DGameRoot : ContextView { void Awake() { context = new DGameContext(this); } }
5.在Game_D文件夹下创建一个文件夹interface,并在interface文件夹下新建一个接口IDoSomething,再新建一个接口实现类DDoSomething,接口实现可用于一些manager脚本
接口:
public interface IDoSomething { void DoSomeFunction(); }
接口实现:
using UnityEngine; using System.Collections; public class DDoSomething : IDoSomething { public void DoSomeFunction() { Debug.Log("interface do something function"); } }
6.在controller文件夹下新建一个脚本DDoSomethingCommand,用来执行实现的接口功能
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,所以需要在controller文件夹下新建一个脚本DGameEvent
public enum DGameEvent { DoSomething, }
8.打开DGameContext脚本,添加以下内容
using UnityEngine; using System.Collections; using strange.extensions.context.impl; using strange.extensions.command.impl; using strange.extensions.context.api; public class DGameContext : MVCSContext { public DGameContext(MonoBehaviour view) : base(view) { } protected override void mapBindings() { base.mapBindings(); injectionBinder.Bind<IDoSomething>().To<DDoSomething>().ToSingleton(); commandBinder.Bind(DGameEvent.DoSomething).To<DDoSomethingCommand>(); commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once(); } }
9.打开DGameCommand脚本,添加以下内容,这里就是接口函数的调用,可以再度运行调试
using strange.extensions.command.impl; using strange.extensions.context.api; using UnityEngine; public class DGameCommand : EventCommand { public override void Execute() { Debug.Log("Start DGame"); dispatcher.Dispatch(DGameEvent.DoSomething); } }
10.在Game_D文件下新建一个文件夹view文件夹,并在view文件夹下新建一个脚本DButtonView继承自EventView
using UnityEngine; using System.Collections; using strange.extensions.mediation.impl; public class DButtonView : EventView { internal const string CLICK_BUTTON = "CLICK_BUTTON"; void OnGUI() { if (GUI.Button(new Rect(0, 0, 100, 30), "Click")) { dispatcher.Dispatch(CLICK_BUTTON); } } }
11.在view文件夹下新建一个脚本DButtonMediator,继承自EventMediator
using UnityEngine; using System.Collections; using strange.extensions.mediation.impl; public class DButtonMediator :EventMediator{ [Inject] public DButtonView btnView{get;set;} public override void OnRegister() { base.OnRegister(); btnView.dispatcher.AddListener(DButtonView.CLICK_BUTTON, OnBtnViewClick); } void OnBtnViewClick() { Debug.Log("click view button"); } }
12.再改一次DGameContext脚本:
using UnityEngine; using System.Collections; using strange.extensions.context.impl; using strange.extensions.command.impl; using strange.extensions.context.api; public class DGameContext : MVCSContext { public DGameContext(MonoBehaviour view) : base(view) { } protected override void mapBindings() { base.mapBindings(); injectionBinder.Bind<IDoSomething>().To<DDoSomething>().ToSingleton(); mediationBinder.Bind<DButtonView>().To<DButtonMediator>(); commandBinder.Bind(DGameEvent.DoSomething).To<DDoSomethingCommand>(); commandBinder.Bind(ContextEvent.START).To<DGameCommand>().Once(); } }
13.最后再在场景中新建一个物体,将DButtonView拖上去,然后运行
14.最后都建议,自己手动写一遍代码,方便理解