首要知道什么是MVCS
1、M 即Model 是本地数据类型 比如从服务器、表、xml等获得的数据赋值给model,以后再取用数据时直接读取model对象即可。一般为单例的对象。
2、V 即View 是视图 一般就是UI这一块,在StrangeIoc中View一般以组件的形式挂在物体身上的,负责查找UI上需要交互的对象。并且在view中提供更新UI的方法。
3、C 即Command 是命令 在Strangeioc中会和事件绑定,绑定后只要触发事件就会执行对应的Command,Command是框架的核心 负责和service、model 以及 Mediator(中间层)的交互。
4、S 即Service 是服务层 属于服务端数据,请求数据,更新数据,保存数据等。
StrangeIoc是一款基于MVCS的一种框架,是对MVC思想的扩展,是专门针对Unity开发的一款框架。下面是官方提供的结构图,类似于UML图。
简单分析
以自己的项目为例:
1、Root 是整个框架的起点,是要挂载在物体上的,继承自ContextView,而ContextView继承自MonoBehaviour,相当于ContextView间接继承自MonoBehaviour,而他的主要功能是创建Context。
public class GameRoot : ContextView {
void Awake()
{
context = new GameContext(this,true);//开启框架
context.Start();
}
}
2、Context这个类继承自MVCSContext,主要功能负责进行绑定 mapBinding。
public class GameContext : MVCSContext {
public GameContext(MonoBehaviour view, bool autoMapping) : base(view, autoMapping)
{
}
/// <summary>
/// 绑定映射
/// </summary>
protected override void mapBindings()
{
Debug.Log("框架启动成功");
base.mapBindings();
//确保所有的其他控制器脚本能获取 IntegrationModel
injectionBinder.Bind<InteGrationModel>().To<InteGrationModel>().ToSingleton();
mediationBinder.Bind<IntegrationView>().To<IntegrationMediator>();
mediationBinder.Bind<CharacterView>().To<CharacterMediator>();
mediationBinder.BindView<StartView>().ToMediator<StartMediator>();
commandBinder.Bind(CommandEvent.ChangeMultiple).To<ChangeMultipleCommand>();
commandBinder.Bind(CommandEvent.GrabLandLord).To<GrabLandlordCommand>();//玩家抢地主
commandBinder.Bind(CommandEvent.GameOver).To<GameOverCommand>();
commandBinder.Bind(CommandEvent.RequestUpdate).To<RequestUpdateCommand>();
commandBinder.Bind(CommandEvent.PlayCard).To<PlayerCardCommand>();
commandBinder.Bind(CommandEvent.PassCard).To<PassCardCommand>();
commandBinder.Bind(NoThanks.PlayerBuchu).To<BuchuUI>();
//开始命令 ContextEvent.START框架封装
commandBinder.Bind(ContextEvent.START).To<StartCommand>().Once();
//确保所有的其他控制器脚本 能获取 CardModel
injectionBinder.Bind<CardModel>().To<CardModel>().ToSingleton();
injectionBinder.Bind<RoundModel>().To<RoundModel>().ToSingleton();
commandBinder.Bind(CommandEvent.RequestDeal).To<RequestDealCommand>();
}
}
从中我们分开来看:
injectionBinder.Bind<InteGrationModel>().To<InteGrationModel>().ToSingleton();
模型数据绑定为单例模式
commandBinder.Bind(CommandEvent.GrabLandLord).To<GrabLandlordCommand>();//玩家抢地主
绑定事件命令 命令可以自定义 当dispatchar这个命令时,GrabLandlordCommand里的execute方法就会执行
mediationBinder.Bind<CharacterView>().To<CharacterMediator>();
视图层和中间层绑定
/