Architecture
Architecture代码
using System;
using System.Collections.Generic;namespace QFramework
{public interface IArchitecture{/// <summary>/// 注册系统/// </summary>void RegisterSystem<T>(T instance) where T : ISystem; // 新增/// <summary>/// 注册 Model/// </summary>void RegisterModel<T>(T instance) where T : IModel;/// <summary>/// 注册 Utility/// </summary>void RegisterUtility<T>(T instance) where T : IUtility;/// <summary>/// 获取系统/// </summary>T GetSystem<T>() where T : class, ISystem;/// <summary>/// 获取数据/// </summary>T GetModel<T>() where T : class, IModel;/// <summary>/// 获取工具/// </summary>T GetUtility<T>() where T : class, IUtility;void SendCommand<T>() where T : ICommand, new();void SendCommand<T>(T command) where T : ICommand;/// <summary>/// 发送事件/// </summary>void SendEvent<T>() where T : new(); /// <summary>/// 发送事件/// </summary>void SendEvent<T>(T e);/// <summary>/// 注册事件/// </summary>IUnRegister RegisterEvent<T>(Action<T> onEvent);/// <summary>/// 注销事件/// </summary>void UnRegisterEvent<T>(Action<T> onEvent);}/// <summary>/// 架构/// </summary>/// <typeparam name="T"></typeparam>public abstract class Architecture<T> : IArchitecture where T : Architecture<T>, new(){/// <summary>/// 是否已经初始化完成/// </summary>private bool mInited = false;/// <summary>/// 用于初始化的 Systems 的缓存/// </summary>private List<ISystem> mSystems = new List<ISystem>(); // 新增// 提供一个注册 Model 的 APIpublic void RegisterSystem<T>(T instance) where T : ISystem // 新增{// 需要给 Model 赋值一下instance.SetArchitecture(this);mContainer.Register<T>(instance);// 如果初始化过了if (mInited){instance.Init();}else{// 添加到 Model 缓存中,用于初始化mSystems.Add(instance);}}/// <summary>/// 用于初始化的 Models 的缓存/// </summary>private List<IModel> mModels = new List<IModel>();public void RegisterModel<T>(T instance) where T : IModel{// 需要给 Model 赋值一下instance.SetArchitecture(this);mContainer.Register<T>(instance);// 如果初始化过了if (mInited){instance.Init();}else{// 添加到 Model 缓存中,用于初始化mModels.Add(instance);}}public T GetModel<T>() where T : class, IModel{return mContainer.Get<T>();}#region 类似单例模式 但是仅在内部课访问/// <summary>/// 注册补丁/// </summary>public static Action<T> OnRegisterPatch = architecture=> { };private static T mArchitecture = null;public static IArchitecture Interface{get {if(mArchitecture == null){MakeSureArchitecture();}return mArchitecture;}}// 确保 Container 是有实例的static void MakeSureArchitecture(){ if (mArchitecture == null){mArchitecture = new T();mArchitecture.Init();// 调用OnRegisterPatch?.Invoke(mArchitecture);// 初始化 Modelforeach (var architectureModel in mArchitecture.mModels){architectureModel.Init();}// 清空 ModelmArchitecture.mModels.Clear();// 初始化 Systemforeach (var architectureSystem in mArchitecture.mSystems) // 新增{architectureSystem.Init();}// 清空 SystemmArchitecture.mSystems.Clear(); // 新增mArchitecture.mInited = true;}}#endregionprivate IOCContainer mContainer = new IOCContainer();// 留给子类注册模块protected abstract void Init();// 提供一个注册模块的 APIpublic static void Register<T>(T instance){MakeSureArchitecture();mArchitecture.mContainer.Register<T>(instance);}public void RegisterUtility<T>(T instance) where T : IUtility{mContainer.Register<T>(instance);}public T GetUtility<T>() where T : class, IUtility{return mContainer.Get<T>();}public T GetSystem<T>() where T : class, ISystem{return mContainer.Get<T>();}// 提供一个获取模块的 APIpublic static T Get<T>() where T : class{MakeSureArchitecture();return mArchitecture.mContainer.Get<T>();}public void SendCommand<T>() where T : ICommand, new(){var command = new T();command.SetArchitecture(this);command.Excute();command.SetArchitecture(null);}public void SendCommand<T>(T command) where T : ICommand{command.SetArchitecture(this);command.Excute();command.SetArchitecture(null);}ITypeEventSystem mtypeEventSystem = new TypeEventSystem();public void SendEvent<T>() where T : new(){mtypeEventSystem.Send<T>();}public void SendEvent<T>(T e){mtypeEventSystem.Send<T>(e);}public IUnRegister RegisterEvent<T>(Action<T> onEvent){return mtypeEventSystem.Register<T>(onEvent);}public void UnRegisterEvent<T>(Action<T> onEvent){mtypeEventSystem.UnRegister<T>(onEvent);}}
}
代码解释
代码非常长,主体是架构,单例的模式,相当于一款游戏中你把这个挂在到总的游戏中,下面你不同层的东西可以用这个架构访问,类似于中转站
用法说明
//各种注册
void RegisterSystem<T>(T instance) where T : ISystem;
void RegisterModel<T>(T instance) where T : IModel;
void RegisterUtility<T>(T instance) where T : IUtility;
//获取方法
T GetModel<T>() where T : class, IModel; // 数据
T GetSystem<T>() where T : class, ISystem; // 系统
T GetUtility<T>() where T : class, IUtility; //工具
// Command用法
void SendCommand<T>() where T : ICommand, new(); // 发送命令
void SendCommand<T>(T command) where T : ICommand; // 发送命令
// Event用法
IUnRegister RegisterEvent<T>(Action<T> onEvent); // 注册事件
void UnRegisterEvent<T>(Action<T> onEvent); // 注销事件
void SendEvent<T>() where T : new(); // 发送事件
void SendEvent<T>(T e); // 发送事件
下面是示例
using System.Diagnostics;
using UnityEngine;
namespace QFramework.E.g{public class PointGame : Architecture<PointGame>{protected override void Init(){RegisterSystem<IAchievementSystem>(new AchievementSystem()); // 注册系统RegisterModel<IGameModel>(new GameModel()); // 注册数据}}
}this.RegisterEvent<GamePassEvent>(OnGamePass);
private void OnGamePass(GamePassEvent e){transform.Find("Canvas/GamePass").gameObject.SetActive(true);
}
this.UnRegisterEvent<GamePassEvent>(OnGamePass);this.RegisterEvent<GamePassEvent>(e =>{transform.Find("Canvas/Show/BestScore").GetComponent<TMP_Text>().text = "BestSocre: " + this.GetModel<IGameModel>().BestScore.Value.ToString();}).UnRegisterWhenGameObjectDestroyed(this.gameObject);
// Command
this.SendEvent<BestScoreEvent>(); // 触发事件