StrangeIoc启动实例,输出HelloWorld

1.创建空物体Root,创建RootContextView挂载上去,用来实例化Context。

//RootContextView.cs文件
using strange.extensions.context.impl;
using UnityEngine;

public class RootContextView : ContextView {

    void Awake() {
		//创建Context
		this.context = new DemoContext (this);
    }

}

2.创建DemoContext类,用来绑定

using UnityEngine;
using strange.extensions.context.impl;//MVCSContext 
using strange.extensions.context.api;//ContextEvent.START

public class DemoContext : MVCSContext {

	public DemoContext(MonoBehaviour view):base(view){}

	protected override void mapBindings ()
	{
		//Binding Todo
		//绑定启动命令,此方法完成后,就会自动分发ContextEvent.START命令
		commandBinder.Bind (ContextEvent.START).To<StartCommand> ().Once();
	}

}

3.创建StartCommand类,作为启动命令。

using UnityEngine;
using strange.extensions.command.impl;

public class StartCommand : Command {

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

4.解说一下,创建RootContextView继承自ContextView(MonoBehaviour的子类),挂载到场景中,自动实例化调用Awake()方法,实例化DemoContext类,DemoContext类继承自MVCSContext,里面重写了mapBindings()方法在Context创建时自动调用,用于绑定关系,最后绑定启动命令到StartCommand,在绑定结束后就会调用这个事件,调用StartCommand的Execute()方法,游戏的起点。


首页 我的博客
粤ICP备17103704号