萌熊跑酷项目-MVC框架搭建
MVC组件类型的关系和功能 如上图
View-视图 抽象类
1、Name 用于各个view之间进行查找、区分-----对应“视图选择”
2、AttentionList 关心事件列表
3、RegisterAttentionEvent 注册关心事件
4、HandleEvent 处理来自Model的事件(参数 事件名称和参数)-----对应“通知改变”
5、SentEvent 发送事件到Controller-----对应“用户请求”
6、GetModel 获取Model-----对应“状态查询”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class View : MonoBehaviour {
//名字标识
public abstract string Name { get; }
//事件关心列表
[HideInInspector]
public List<string> AttentionList = new List<string>();
public virtual void RegisterAttentionEvent() { }
//处理事件
public abstract void HandleEvent(string name,object data);
//发送事件
protected void SendEvent(string eventName,object data=null) {
MVC.SendEvent(eventName,data);
}
//获取Model
protected T GetModel<T>()
where T:Model
{
return MVC.GetModel<T>() as T;
}
}
Model 数据模型
1、Name 名字,用于区分-----对应“状态改变”
2、SendEvent 发送事件到view(通过MVC这个中介者)-----对应“通知改变”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Model {
//名字标识
public abstract string Name { get; }
//发送事件
protected void SendEvent(string eventName,object data=null)
{
MVC.SendEvent(eventName,data);
}
}
Controller
1、Execute 执行事件
2、GetModel GetView 获取Model和View
3、注册model、view、controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public abstract class Controller {
//执行事件
public abstract void Execute(object data);
//获取模型
protected T GetModel<T>()
where T:Model
{
return MVC.GetModel<T>() as T;
}
//获取视图
protected T GetView<T>()
where T : View
{
return MVC.GetView<T>() as T;
}
//注册模型
protected void RegisterModel(Model model) {
MVC.RegisterModel(model);
}
//注册视图
protected void RegisterView(View view) {
MVC.RegisterView(view);
}
//注册Controller
protected void RegisterController(string eventName,Type controllerType) {
MVC.RegisterController(eventName,controllerType);
}
}
MVC 中介者
1、使用字典存储所有的model、view、controller
2、注册所有的model、view、controller(即将model、view、controller存入字典)
3、获取model、view(即从字典中取值,,,所有取值使用之前,必须先注册进字典中)
4、发送事件(mvc框架通过controller执行事件,动态生成对应的controller,所以不同的事件对应不同的controller,执行完事件后,view做对应操作)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MVC {
//资源
public static Dictionary<string, Model> Models = new Dictionary<string, Model>();
public static Dictionary<string, View> Views = new Dictionary<string, View>();
public static Dictionary<string, Type> CommandMap = new Dictionary<string, Type>();
//注册View
public static void RegisterView(View view) {
//防止view重复注册
if (Views.ContainsKey(view.name))
{
Views.Remove(view.name);
}
view.RegisterAttentionEvent();
Views[view.Name] = view;
}
//注册model
public static void RegisterModel(Model model) {
Models[model.Name] = model;
}
//注册controller
public static void RegisterController(string eventName,Type controllerType) {
CommandMap[eventName] = controllerType;
}
//获取model
public static T GetModel<T>()
where T : Model
{
foreach (var m in Models.Values)
{
if (m is T)
{
return (T)m;
}
}
return null;
}
//获取view
public static T GetView<T>()
where T : View
{
foreach (var v in Views.Values)
{
if (v is T)
{
return (T)v;
}
}
return null;
}
//发送事件
public static void SendEvent(string eventName,object data=null) {
//controller执行
if (CommandMap.ContainsKey(eventName))
{
Type t = CommandMap[eventName];
//控制器生成 Activator反射类 传入类型
Controller c = Activator.CreateInstance(t) as Controller;
c.Execute(data);
}
//view处理
foreach (var v in Views.Values)
{
if (v.AttentionList.Contains(eventName))
{
//执行
v.HandleEvent(eventName,data);
}
}
}
}
Game 开启框架
1、对象池、音乐管理、静态数据等单例的全局访问点
2、注册游戏开始事件
3、发送游戏开始事件
4、进入新场景
在场景1中完成框架初始化,之后跳转场景4中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
[RequireComponent(typeof(ObjectPool))]
[RequireComponent(typeof(Sound))]
[RequireComponent(typeof(StaticData))]
public class Game : MonoSingleton<Game> {
//全局访问
[HideInInspector]
public ObjectPool objectPool;
[HideInInspector]
public Sound sound;
[HideInInspector]
public StaticData staticData;
private void Start()
{
//
DontDestroyOnLoad(gameObject);
objectPool = ObjectPool.Instance;
sound = Sound.Instance;
staticData = StaticData.Instance;
//初始化注册startUpcontroller
RegisterController(Consts.E_StartUp, typeof(StartUpController));
//游戏启动
SendEvent(Consts.E_StartUp);
//跳转场景
Game.Instance.LoadLevel(4);
}
public void LoadLevel(int level) {
//发送退出场景事件
ScenesArgs e = new ScenesArgs()
{
//获取当前场景索引
scnesIndex = SceneManager.GetActiveScene().buildIndex
};
SendEvent(Consts.E_ExitScenes,e);
//发送加载新的场景事件
SceneManager.LoadScene(level,LoadSceneMode.Single);
}
//进入新场景
private void OnLevelWasLoaded(int level)
{
Debug.Log("进入新场景:"+ level);
//发送进入场景事件
ScenesArgs e = new ScenesArgs()
{//获取当前场景索引
scnesIndex = level
};
SendEvent(Consts.E_EnterScenes, e);
}
//发送事件
void SendEvent(string eventName, object data = null)
{
MVC.SendEvent(eventName, data);
}
//注册controler
void RegisterController(string eventName, Type controllerType)
{
MVC.RegisterController(eventName, controllerType);
}
}
StartUpController
1、注册所有的controller(除了StartUpController在Game中注册)
2、注册Model,并初始化
此处首先注册了进入
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartUpController : Controller
{
public override void Execute(object data)
{
//注册所有的controller
RegisterController(Consts.E_EnterScenes,typeof(EnterScenesController));
//注册model
//初始化Model
}
}