基于MVCS模式的StrangeIoc框架
strangeIoc框架是专为Unity写的一套游戏开发框架。
框架的作用是方便项目的管理,随着项目增大,没有框架的话,后期很难管理。
Unity开发注重逻辑和UI的分离,UI只负责UI方面,不牵扯代码逻辑,不至于最终混为一团,不利于后期开发和维护。
strangeIoc框架是基于MVCS模式,在MVC的基础上多出了service一层,是MVC框架的变种。完全使UI和逻辑分离。
先来看一下,总体逻辑图。
1.Root 是整个框架的启动部分,挂载场景物体上
/// <summary>
/// Root
/// 启动整个框架
/// </summary>
public class Demo1ContextView : ContextView {
private void Awake()
{
this.context = new Demo1Context(this);
}
}
2.整个框架代码耦合部分在Context中,负责绑定一些命令,通过绑定命令,简介执行代码。在代码中 只需发起命令,程序
会根据事先绑定好的方法,进行调用。
public class Demo1Context : MVCSContext {
public Demo1Context(MonoBehaviour view) : base(view)
{
}
//进行绑定映射
protected override void mapBindings()
{
//model
injectionBinder.Bind<ScoreModel>().To<ScoreModel>().ToSingleton();
//command
commandBinder.Bind(Demo1CommandEvent.REQUESTSCORE).To<RequestScoreCom>();
commandBinder.Bind(Demo1CommandEvent.UPDATESCORE).To<UpdateScoreCommand>();
//service
injectionBinder.Bind<IScoreService>().To<ScoreService>().ToSingleton();//表示对象只会在工程生成一次
//mediator
mediationBinder.Bind<CubeView>().To<CubeMediator>(); //完成view 和mediator 绑定
//创建一个StartCommand 绑定开始事件 只触发一次
commandBinder.Bind(ContextEvent.START).To<StartCommand>().Once();
}
}
以上绑定分为三种方式:1.commandBinder.Bind,中间命令的绑定的方式。
2. mediationBinder.Bind,中间层的绑定
3.injectionBinder.Bind,服务端和数据模型之间绑定方式
3.mediator 类是UI和逻辑之间的交互中间层。UI和逻辑层只需要通过发起Command命令,就可以达到目的。
相应的制定mediator层的命令用于交互
/// <summary>
/// UI向mediator发起的命令
/// </summary>
public enum Demo1MediatorEvent
{
SCORECHANGE,
CLICKDOWN
}
/// <summary>
/// mediator层向服务端请求数据命令
/// </summary>
public enum Demo1CommandEvent
{
REQUESTSCORE,
UPDATESCORE
}
//mediator层代码
public class CubeMediator : Mediator {
//注入
[Inject]
public CubeView cubeView { set; get; }
[Inject(ContextKeys.CONTEXT_DISPATCHER)] //全局的Dispatcher
public IEventDispatcher dispatcher { get; set; }
public override void OnRegister()
{
cubeView.Init();
dispatcher.AddListener(Demo1MediatorEvent.SCORECHANGE,OnScoreChange);
cubeView.dispatcher.AddListener(Demo1MediatorEvent.CLICKDOWN, ClickDown);
//通过Dispatcher 发起请求分数的命令
dispatcher.Dispatch(Demo1CommandEvent.REQUESTSCORE);
}
public override void OnRemove()
{
Debug.Log("OnRemove");
dispatcher.RemoveListener(Demo1MediatorEvent.SCORECHANGE, OnScoreChange);
dispatcher.RemoveListener(Demo1MediatorEvent.CLICKDOWN, ClickDown);
}
public void OnScoreChange(IEvent evt)
{
cubeView.UpdateScore((int)evt.data);
}
public void ClickDown()
{
dispatcher.Dispatch(Demo1CommandEvent.UPDATESCORE);
}
}
//UI层
public class CubeView : View {
[Inject]
public IEventDispatcher dispatcher { get; set; }
private Text _scoreText;
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
_scoreText = this.GetComponentInChildren<Text>();
}
private void Update()
{
transform.Translate(new Vector3(Random.Range(-1, 2),
Random.Range(-1, 2), Random.Range(-1, 2)) * 0.1f);
}
private void OnMouseDown()
{
Debug.Log("加分");
dispatcher.Dispatch(Demo1MediatorEvent.CLICKDOWN);
}
public void UpdateScore(int score)
{
_scoreText.text = score.ToString();
}
}
4.请求分数和更新分数
public class RequestScoreCom : EventCommand {
[Inject]
public IScoreService scroService { get; set; }
[Inject]
public ScoreModel scoreModel { get; set; }
public override void Execute()
{
Retain();//命令是一次性的,所以要保存
scroService.dispatcher.AddListener(Demo1ServiceEvent.REQUESTSCORE, OnComplete);
scroService.RequestScore("http://xxx.xxx.xx");
}
private void OnComplete(IEvent evt)
{
scroService.dispatcher.RemoveListener(Demo1ServiceEvent.REQUESTSCORE, OnComplete);
//把数据保存到数据模型中去
scoreModel.Score = (int)evt.data;
dispatcher.Dispatch(Demo1MediatorEvent.SCORECHANGE, evt.data);
Release(); //到这里使用完之后 在释放命令
}
}
public class UpdateScoreCommand :EventCommand {
[Inject]
public ScoreModel scoreModel { set; get; }
[Inject]
public IScoreService scoreService { get; set; }
public override void Execute()
{
scoreModel.Score++;
scoreService.UpdateScore("http://xxx.xxx.xx", scoreModel.Score);
dispatcher.Dispatch(Demo1MediatorEvent.SCORECHANGE, scoreModel.Score);
}
}
5.服务端随机一个分数
服务端继承接口IService
public interface IScoreService
{
//向服务器请求分数
void RequestScore(string url);
//收到服务器发送过来的分数
void OnReceiveScore();
//向服务器提交最新分数
void UpdateScore(string url, int score);
IEventDispatcher dispatcher { get; set; }
}
服务端
public class ScoreService : IScoreService
{
[Inject]
public IEventDispatcher dispatcher { get; set; }
public void RequestScore(string url)
{
OnReceiveScore();
}
public void OnReceiveScore()
{
int score = Random.Range(0, 100);
dispatcher.Dispatch(Demo1ServiceEvent.REQUESTSCORE, score);
}
public void UpdateScore(string url, int score)
{
Debug.Log("Update Score :" + score + "to url :" + url);
}
}
以上就是对strangeioc框架的初步学习。