源码地址: http://pan.baidu.com/s/1hrApFFI (注 : 当时没写完的Demo工程,主要看看MVC分层)
应用场景: 开始关卡
顺序逻辑:Viiew的Mediator里监听开始关卡按钮 -》 把参数传递发出开始关卡事件 -》 Command与Model交互并读取参数开始相应关卡
1.Mediator代码
public class Mediator_Select : EventMediator {
[Inject]
public View_Select View_Select { get; set; }
/// <summary>
/// 监听初始化
/// </summary>
public override void OnRegister()
{
View_Select.Init();
View_Select.BtnStart.onClick.AddListener(OnBtnStartClick);
View_Select.BtnHelp.onClick.AddListener(OnBtnHelpClick);
View_Select.BtnBack.onClick.AddListener(OnBtnBtnBackClick);
dispatcher.AddListener(ViewEventType.InitLevelCard, OnInitLevelCard);
}
public void OnDestroy()
{
View_Select.BtnStart.onClick.RemoveListener(OnBtnStartClick);
View_Select.BtnHelp.onClick.RemoveListener(OnBtnHelpClick);
View_Select.BtnBack.onClick.RemoveListener(OnBtnBtnBackClick);
dispatcher.RemoveListener(ViewEventType.InitLevelCard, OnInitLevelCard);
}
/// <summary>
/// 传递参数并发出事件
/// </summary>
void OnBtnStartClick()
{
Args_StartLevel e = new Args_StartLevel() { LevelIndex = View_Select.m_selectIndex };
dispatcher.Dispatch(EventType.StartLevel,e);
}
void OnBtnBtnBackClick()
{
Game.Instance.LoadScene(1);
}
void OnBtnHelpClick()
{
}
void OnInitLevelCard(IEvent e)
{
Args_InitLevelCard ee = e.data as Args_InitLevelCard;
View_Select.LoadCardLevels(ee.levels, ee.GameProgress);
}
}
public class Command_StartLevel : EventCommand {
[Inject]
public Model_Game Model_Game { get; set; }
public override void Execute()
{
Args_StartLevel e = evt.data as Args_StartLevel;
Model_Game.StartLevel(e.LevelIndex);
Game.Instance.LoadScene(3);
}
}