BaseComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Engine.Component
  6. {
  7. /// <summary>游戏组件基类</summary>
  8. public class BaseComponent : MonoBehaviour
  9. {
  10. /// <summary>组件是否被销毁</summary>
  11. protected bool IsDestroy = false;
  12. /// <summary>组件是否被释放</summary>
  13. protected bool IsDispose = false;
  14. /// <summary>是否为空闲状态</summary>
  15. public bool IsReset = false;
  16. /// <summary>初始化函数</summary>
  17. public virtual void Awake()
  18. {
  19. CacheComponents(this);
  20. }
  21. /// <summary>重置组件,清空组件对外的所用引用,如果需要组件重用,需要在缓存前调用次函数</summary>
  22. public virtual void Reset()
  23. {
  24. IsReset = true;
  25. this.gameObject.SetActive(false);
  26. }
  27. /// <summary>开始使用</summary>
  28. public virtual void StartUse()
  29. {
  30. IsReset = false;
  31. this.gameObject.SetActive(true);
  32. }
  33. /// <summary>获得gameObject对象</summary>
  34. public virtual GameObject GetGameObject()
  35. {
  36. return this.gameObject;
  37. }
  38. /// <summary>释放函数,调用此函数后组件不可再用</summary>
  39. public virtual void Dispose()
  40. {
  41. //修改释放标记
  42. IsDispose = true;
  43. //是否需要调用销毁
  44. if (!IsDestroy)
  45. {
  46. Destroy(this);
  47. }
  48. }
  49. /// <summary>组件被销毁时调用,Unity3D底层API自动触发</summary>
  50. private void OnDestroy()
  51. {
  52. DelComponents(this);
  53. //修改销毁标记
  54. IsDestroy = true;
  55. //是否需要调用释放函数
  56. if (!IsDispose)
  57. {
  58. this.Dispose();
  59. }
  60. }
  61. private string mComponentKey = string.Empty;
  62. /// <summary>组件缓存键值</summary>
  63. public string ComponentKey
  64. {
  65. get
  66. {
  67. if (mComponentKey == string.Empty)
  68. {
  69. mComponentKey = GetComponentKey (this.GetGameObject());
  70. }
  71. return mComponentKey;
  72. }
  73. }
  74. public static Dictionary<string, BaseComponent> DictBaseComponents = new Dictionary<string, BaseComponent>();
  75. /// <summary>缓存特效</summary>
  76. protected static void CacheComponents(BaseComponent baseComponents)
  77. {
  78. if (!DictBaseComponents.ContainsKey(baseComponents.ComponentKey))
  79. {
  80. DictBaseComponents.Add(baseComponents.ComponentKey, baseComponents);
  81. }
  82. else
  83. {
  84. DictBaseComponents[baseComponents.ComponentKey] = baseComponents;
  85. }
  86. }
  87. /// <summary>查找组件</summary>
  88. public static BaseComponent FindComponents(GameObject componentObj)
  89. {
  90. string componentKey = GetComponentKey (componentObj);
  91. if (DictBaseComponents.ContainsKey(componentKey))
  92. {
  93. return DictBaseComponents[componentKey];
  94. }
  95. else
  96. {
  97. return null;
  98. }
  99. }
  100. /// <summary>删除缓存</summary>
  101. protected static void DelComponents(BaseComponent baseComponents)
  102. {
  103. if (!DictBaseComponents.ContainsKey(baseComponents.ComponentKey))
  104. {
  105. DictBaseComponents.Remove(baseComponents.ComponentKey);
  106. }
  107. }
  108. /// <summary>获取某个GameObject上某个组件的键值</summary>
  109. protected static string GetComponentKey(GameObject componentObj)
  110. {
  111. //StringBuilder strBuillder = new StringBuilder ();
  112. //strBuillder.Append (componentObj.GetInstanceID());
  113. //strBuillder.Append (componentType.ToString());
  114. return componentObj.gameObject.GetInstanceID().ToString();//strBuillder.ToString ();
  115. }
  116. }
  117. }