DisplayCell.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using UnityEngine;
  2. using System;
  3. using CommonLang.Concurrent;
  4. using CommonAI.Zone;
  5. using System.IO;
  6. using CommonAIClient.Unity.Utils;
  7. using CommonLang;
  8. namespace CommonAIClient.Unity.Battle
  9. {
  10. public class DisplayCell : IDisposable
  11. {
  12. #region RefCount
  13. private static AtomicInteger s_alloc_count = new AtomicInteger(0);
  14. private static AtomicInteger s_active_count = new AtomicInteger(0);
  15. /// <summary>
  16. /// 分配实例数量
  17. /// </summary>
  18. public static int AllocCount { get { return s_alloc_count.Value; } }
  19. /// <summary>
  20. /// 未释放实例数量
  21. /// </summary>
  22. public static int ActiveCount { get { return s_active_count.Value; } }
  23. #endregion
  24. /// <summary>
  25. /// 主体上的挂载列表
  26. /// </summary>
  27. private HashMap<string, DisplayCell> mAttachParts = new HashMap<string, DisplayCell>();
  28. private bool mDisposed = false;
  29. private bool mActiveSelf;
  30. private GameObject mRootNode;
  31. private AssetObjectExt mModel;
  32. private Animation mAnimation;
  33. //保存最后申请的动作
  34. private string mLastAnimName;
  35. private bool mLastCrossFade;
  36. private WrapMode mLastWrapMode;
  37. private float mLastSpeed;
  38. public bool IsDisposed { get { return mDisposed; } }
  39. public string LastAnimName { get { return mLastAnimName; } }
  40. public bool LastCrossFade { get { return mLastCrossFade; } }
  41. public WrapMode LastWrapMode { get { return mLastWrapMode; } }
  42. public float LastSpeed { get { return mLastSpeed; } }
  43. public AssetObjectExt Model
  44. {
  45. set
  46. {
  47. mModel = value;
  48. mModel.gameObject.ParentRoot(mRootNode);
  49. mAnimation = mModel.GetComponent<Animation>();
  50. PlayAnim(LastAnimName, LastCrossFade, LastWrapMode, LastSpeed);
  51. }
  52. }
  53. public bool ActiveSelf
  54. {
  55. get { return mActiveSelf; }
  56. set
  57. {
  58. this.mActiveSelf = value;
  59. this.mRootNode.SetActive(mActiveSelf);
  60. }
  61. }
  62. public DisplayCell(GameObject root, string name = "DisplayCell")
  63. {
  64. mRootNode = new GameObject(name);
  65. if (root != null)
  66. {
  67. mRootNode.ParentRoot(root);
  68. }
  69. s_alloc_count++;
  70. s_active_count++;
  71. }
  72. ~DisplayCell()
  73. {
  74. s_alloc_count--;
  75. }
  76. public DisplayCell AttachPart(string name, string dummy)
  77. {
  78. GameObject dummyNode = GetDummyNode(dummy);
  79. if (dummyNode == null)
  80. {
  81. dummyNode = mRootNode;
  82. }
  83. DisplayCell displayCell = BattleFactroy.Instance.CreateDisplayCell(dummyNode, name);
  84. mAttachParts.Add(name.ToLower(), displayCell);
  85. return displayCell;
  86. }
  87. public void DetachPart(string name)
  88. {
  89. DisplayCell displayCell = mAttachParts.RemoveByKey(name.ToLower());
  90. if (displayCell != null)
  91. {
  92. displayCell.Dispose();
  93. }
  94. }
  95. public virtual GameObject GetDummyNode(string name)
  96. {
  97. if (mModel == null || string.IsNullOrEmpty(name))
  98. {
  99. return null;
  100. }
  101. GameObject dummyNode = mModel.GetDummyNode(name);
  102. if (dummyNode == null)
  103. {
  104. foreach (var elem in mAttachParts)
  105. {
  106. dummyNode = elem.Value.GetDummyNode(name);
  107. if (dummyNode != null)
  108. break;
  109. }
  110. }
  111. return dummyNode;
  112. }
  113. public float GetAnimCurrentTime()
  114. {
  115. if (!string.IsNullOrEmpty(mLastAnimName)
  116. && mAnimation != null)
  117. {
  118. AnimationState state = mAnimation[mLastAnimName];
  119. if (state != null)
  120. {
  121. return state.time;
  122. }
  123. }
  124. return 0f;
  125. }
  126. public float GetAnimLength(string name)
  127. {
  128. if (!string.IsNullOrEmpty(name) && mAnimation != null
  129. && mAnimation[name] != null)
  130. {
  131. return mAnimation[name].length;
  132. }
  133. return 0f;
  134. }
  135. public virtual void PlayAnim(string name, bool crossFade
  136. , WrapMode wrapMode = WrapMode.Once, float speed = 1f, float currentTime = 0f)
  137. {
  138. if (!string.IsNullOrEmpty(name))
  139. {
  140. foreach (var elem in mAttachParts)
  141. {
  142. elem.Value.PlayAnim(name, crossFade, wrapMode, speed, currentTime);
  143. }
  144. mLastAnimName = name;
  145. mLastCrossFade = crossFade;
  146. mLastWrapMode = wrapMode;
  147. mLastSpeed = speed;
  148. if (mAnimation != null)
  149. {
  150. AnimationState state = mAnimation[name];
  151. if (state != null)
  152. {
  153. state.time = 0;
  154. state.wrapMode = wrapMode;
  155. state.speed = speed;
  156. state.layer = 1;
  157. state.time = currentTime;
  158. if (crossFade)
  159. {
  160. mAnimation.CrossFade(name, 0.1f);
  161. }
  162. else
  163. {
  164. mAnimation.Play(name, PlayMode.StopAll);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. public void Update(float deltaTime)
  171. {
  172. if (!mDisposed)
  173. {
  174. OnUpdate(deltaTime);
  175. }
  176. }
  177. protected virtual void OnUpdate(float deltaTime)
  178. {
  179. }
  180. public void Dispose()
  181. {
  182. if (!mDisposed)
  183. {
  184. OnDispose();
  185. mDisposed = true;
  186. s_active_count--;
  187. }
  188. }
  189. protected virtual void OnDispose()
  190. {
  191. foreach (var elem in mAttachParts)
  192. {
  193. elem.Value.Dispose();
  194. }
  195. mAttachParts.Clear();
  196. BattleFactroy.Instance.GameObjectAdapter.Unload(mModel);
  197. GameObject.DestroyObject(mRootNode);
  198. }
  199. }
  200. }