SkillName3D.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4. public class SkillName3D
  5. {
  6. public const string HUD_SKILLNAME_PREFAB = "Battle/SkillNameText";
  7. private TextMeshPro mSkillName;
  8. private GameObject mHud;
  9. private Fighter mFighter = null;
  10. Vector3 m_bloodPos;
  11. public void Born(Fighter owner)
  12. {
  13. mFighter = owner;
  14. mHud = null;
  15. //EventMgr.AddEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
  16. }
  17. public void Dispose()
  18. {
  19. if (mHud != null)
  20. {
  21. ResourceMgr.Instance.RecycleGO(Constants.UI3DPath, HUD_SKILLNAME_PREFAB, mHud);
  22. }
  23. mHud = null;
  24. }
  25. public void Destroy()
  26. {
  27. //EventMgr.RemoveEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
  28. Dispose();
  29. }
  30. public void Prepare()
  31. {
  32. InitHudUI();
  33. }
  34. public void Fight()
  35. {
  36. #if UNITY_EDITOR
  37. if (mHud)
  38. {
  39. mHud.name = mFighter.Ctrl.name + "_skillNameHud";
  40. }
  41. #endif
  42. ResetHudUI();
  43. }
  44. public void Deactive()
  45. {
  46. SetVisible(false);
  47. }
  48. public void OnActorDead()
  49. {
  50. if (mFighter == null) return;
  51. SetVisible(false);
  52. }
  53. public void OnActorRevive()
  54. {
  55. if (mFighter == null) return;
  56. SetVisible(false);
  57. }
  58. public void SetVisible(bool vis)
  59. {
  60. if (mHud == null) return;
  61. mHud.SetActive(vis);
  62. }
  63. public void LateUpdate()
  64. {
  65. if (mHud == null) return;
  66. if (mFighter == null)
  67. {
  68. ResourceMgr.Instance.RecycleGO(Constants.UI3DPath, HUD_SKILLNAME_PREFAB, mHud);
  69. mHud = null;
  70. return;
  71. }
  72. // 这几种情况不需要更新下面的东西,太费了
  73. if (!mFighter.IsVisible)
  74. {
  75. return;
  76. }
  77. Vector3 curUIPointPos = mFighter.Ctrl.GetUIPointPos();
  78. // Update Hud
  79. if (mHud.activeSelf && Camera.main!=null && Camera.main.enabled)
  80. {
  81. Vector3 hudPosition = curUIPointPos + mFighter.Actor.AvatarData.skillNamePos;
  82. Vector3 curBloodPos = Camera.main.WorldToScreenPoint(hudPosition);
  83. bool isNeedUpdateUIHud = (m_bloodPos != curBloodPos);
  84. m_bloodPos = curBloodPos;
  85. if (isNeedUpdateUIHud)
  86. {
  87. UpdateUIHud(ref curBloodPos);
  88. }
  89. }
  90. }
  91. ///-------------------------------------------------
  92. /// 初始化Hud UI
  93. ///-------------------------------------------------
  94. private void InitHudUI()
  95. {
  96. if (mFighter == null)
  97. return;
  98. if (mHud == null)
  99. {
  100. mHud = ResourceMgr.Instance.GetGoFromPool(Constants.UI3DPath, HUD_SKILLNAME_PREFAB);
  101. DebugHelper.Assert(mHud != null, "wtf?");
  102. if (mHud == null)
  103. {
  104. return;
  105. }
  106. Transform hudPanel = GetHudPanel();
  107. if (hudPanel != null)
  108. {
  109. mHud.transform.SetParent(hudPanel, true);
  110. mHud.transform.localScale = Vector3.one;
  111. mHud.transform.localRotation = Quaternion.identity;
  112. }
  113. mSkillName = mHud.transform.Find("name").GetComponent<TextMeshPro>();
  114. }
  115. SetVisible(false);
  116. }
  117. private void ResetHudUI()
  118. {
  119. if (mHud == null || mFighter == null)
  120. {
  121. return;
  122. }
  123. //添加到对应的Panel
  124. Transform hudPanel = GetHudPanel();
  125. if (hudPanel != null)
  126. {
  127. mHud.transform.SetParent(hudPanel, true);
  128. mHud.transform.localScale = Vector3.one;
  129. mHud.transform.localRotation = Quaternion.identity;
  130. }
  131. //Camera_UI3D.GetInstance().GetCurrentCanvas().RefreshLayout();
  132. SetVisible(false);
  133. }
  134. ///-------------------------------------------------
  135. /// 更新UI Hud
  136. ///-------------------------------------------------
  137. private void UpdateUIHud(ref Vector3 bloodPosition)
  138. {
  139. if (mHud != null)
  140. {
  141. bloodPosition.Set(bloodPosition.x, bloodPosition.y, bloodPosition.z);
  142. var UI3D_Came = Camera_UI3D.GetInstance().GetCurrentCamera();
  143. if (UI3D_Came == null)
  144. {
  145. return;
  146. }
  147. mHud.transform.position = UI3D_Came.ScreenToWorldPoint(bloodPosition);
  148. }
  149. }
  150. private Transform GetHudPanel()
  151. {
  152. if (null == BattleCamera.Instance.RealCamera)
  153. {
  154. return null;
  155. }
  156. string UNKNOWN_PANEL = "Unknown_Panel";
  157. if (null == BattleCamera.Instance.RealCamera)
  158. {
  159. return null;
  160. }
  161. var UI3D_Came = Camera_UI3D.GetInstance().GetCurrentCamera();
  162. if (UI3D_Came == null)
  163. {
  164. return null; //有可能该局已经结束了,此时还在loading协程
  165. }
  166. Transform root = UI3D_Came.transform.Find("Hud");
  167. if (root == null)
  168. {
  169. GameObject hud = new GameObject("Hud");
  170. root = hud.transform;
  171. root.parent = UI3D_Came.transform;
  172. root.localPosition = Vector3.zero;
  173. root.localRotation = Quaternion.identity;
  174. root.localScale = Vector3.one;
  175. string[] objNames = new string[5] { "Panel_MonsterHud", "Panel_HeroHud", "FloatTexts", "SkillNames", UNKNOWN_PANEL };
  176. for (int i = 0; i < objNames.Length; ++i)
  177. {
  178. GameObject childGO = new GameObject(objNames[i]);
  179. Transform childTrans = childGO.transform;
  180. childTrans = childGO.transform;
  181. childTrans.SetParent(root);
  182. childTrans.localPosition = Vector3.zero;
  183. childTrans.localRotation = Quaternion.identity;
  184. childTrans.localScale = Vector3.one;
  185. }
  186. }
  187. Transform child = root.Find("SkillNames");
  188. if (child == null)
  189. {
  190. DebugHelper.LogError("WTF????");
  191. }
  192. return child;
  193. }
  194. private void OnShowSkillName(CoreEvent<Fighter> ce)
  195. {
  196. Fighter fighter = ce.Data;
  197. if (fighter != mFighter) return;
  198. string skillName = ce.Param.ToString();
  199. mSkillName.text = skillName;
  200. SetVisible(true);
  201. TimerManager.Instance.AddTimer(2000, 1, OnHideSkillNameTimer);
  202. }
  203. private void OnHideSkillNameTimer(int seq)
  204. {
  205. SetVisible(false);
  206. }
  207. }