using System; using System.Reflection; namespace IFramework { /// /// 模块 /// public abstract class Module : Unit { /// /// 默认名字 /// public const string defaultName = "default"; /// /// 阻止 New /// protected Module() { } /// /// 创建实例 /// /// 模块类型 /// 模块名称 /// /// public static Module CreatInstance(Type type, string name = defaultName, int priority = 0) { Module moudle = Activator.CreateInstance(type) as Module; if (moudle != null) { moudle._binded = false; moudle.name = name; moudle._priority = moudle.OnGetDefautPriority().value + priority; moudle.Awake(); if (moudle is UpdateModule) { (moudle as UpdateModule).enable = true; } } else Log.E(string.Format("Type: {0} Non Public Ctor With 0 para Not Find", type)); return moudle; } /// /// 设置优先级 /// /// protected virtual ModulePriority OnGetDefautPriority() { return ModulePriority.Custom; } /// /// 创建实例 /// /// 模块名称 /// /// public static T CreatInstance(string name = defaultName, int priority = 0) where T : Module { return CreatInstance(typeof(T), name, priority) as T; } /// /// 绑定模块容器 /// /// public void Bind(IModules container) { if (this._container != null) { Log.E(string.Format("Have Bind One Container chunck: You Can UnBind First")); return; } if ((container as Modules).SubscribeModule(this)) { this._binded = true; this._container = container; } } /// /// 解除绑定模块容器 /// /// public void UnBind(bool dispose = true) { if (!binded) return; if (binded && this._container != null) { (this._container as Modules).UnSubscribeBindModule(this); this._binded = false; this._container = null; } if (dispose) Dispose(); } private IModules _container; private bool _binded; private int _priority; /// /// 优先级(越大释放越早释放,越小越先 update) /// public int priority { get { return _priority; } } /// /// 是否绑定了 /// public bool binded { get { return _binded; } } /// /// 模块所处的容器 /// public IModules container { get { return _container; } } /// /// 名字 /// public string name { get; set; } /// /// 释放 /// public override void Dispose() { UnBind(false); base.Dispose(); } /// /// 初始化 /// protected abstract void Awake(); } }