Module.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Reflection;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// 模块
  7. /// </summary>
  8. public abstract class Module : Unit
  9. {
  10. /// <summary>
  11. /// 默认名字
  12. /// </summary>
  13. public const string defaultName = "default";
  14. /// <summary>
  15. /// 阻止 New
  16. /// </summary>
  17. protected Module() { }
  18. /// <summary>
  19. /// 创建实例
  20. /// </summary>
  21. /// <param name="type">模块类型</param>
  22. /// <param name="name">模块名称</param>
  23. /// <param name="priority"></param>
  24. /// <returns></returns>
  25. public static Module CreatInstance(Type type, string name = defaultName, int priority = 0)
  26. {
  27. Module moudle = Activator.CreateInstance(type) as Module;
  28. if (moudle != null)
  29. {
  30. moudle._binded = false;
  31. moudle.name = name;
  32. moudle._priority = moudle.OnGetDefautPriority().value + priority;
  33. moudle.Awake();
  34. if (moudle is UpdateModule)
  35. {
  36. (moudle as UpdateModule).enable = true;
  37. }
  38. }
  39. else
  40. Log.E(string.Format("Type: {0} Non Public Ctor With 0 para Not Find", type));
  41. return moudle;
  42. }
  43. /// <summary>
  44. /// 设置优先级
  45. /// </summary>
  46. /// <returns></returns>
  47. protected virtual ModulePriority OnGetDefautPriority()
  48. {
  49. return ModulePriority.Custom;
  50. }
  51. /// <summary>
  52. /// 创建实例
  53. /// </summary>
  54. /// <param name="name">模块名称</param>
  55. /// <param name="priority"></param>
  56. /// <returns></returns>
  57. public static T CreatInstance<T>(string name = defaultName, int priority = 0) where T : Module
  58. {
  59. return CreatInstance(typeof(T), name, priority) as T;
  60. }
  61. /// <summary>
  62. /// 绑定模块容器
  63. /// </summary>
  64. /// <param name="container"></param>
  65. public void Bind(IModules container)
  66. {
  67. if (this._container != null)
  68. {
  69. Log.E(string.Format("Have Bind One Container chunck: You Can UnBind First"));
  70. return;
  71. }
  72. if ((container as Modules).SubscribeModule(this))
  73. {
  74. this._binded = true;
  75. this._container = container;
  76. }
  77. }
  78. /// <summary>
  79. /// 解除绑定模块容器
  80. /// </summary>
  81. /// <param name="dispose"></param>
  82. public void UnBind(bool dispose = true)
  83. {
  84. if (!binded) return;
  85. if (binded && this._container != null)
  86. {
  87. (this._container as Modules).UnSubscribeBindModule(this);
  88. this._binded = false;
  89. this._container = null;
  90. }
  91. if (dispose)
  92. Dispose();
  93. }
  94. private IModules _container;
  95. private bool _binded;
  96. private int _priority;
  97. /// <summary>
  98. /// 优先级(越大释放越早释放,越小越先 update)
  99. /// </summary>
  100. public int priority { get { return _priority; } }
  101. /// <summary>
  102. /// 是否绑定了
  103. /// </summary>
  104. public bool binded { get { return _binded; } }
  105. /// <summary>
  106. /// 模块所处的容器
  107. /// </summary>
  108. public IModules container { get { return _container; } }
  109. /// <summary>
  110. /// 名字
  111. /// </summary>
  112. public string name { get; set; }
  113. /// <summary>
  114. /// 释放
  115. /// </summary>
  116. public override void Dispose()
  117. {
  118. UnBind(false);
  119. base.Dispose();
  120. }
  121. /// <summary>
  122. /// 初始化
  123. /// </summary>
  124. protected abstract void Awake();
  125. }
  126. }