BattleFloatBuffText.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class BattleFloatBuffText : MonoBehaviour
  5. {
  6. public Transform item;
  7. Fighter target = null;
  8. bool bDisposed = false;
  9. bool bInited = false;
  10. Image functionImg;
  11. GameObject missGo;
  12. GameObject dodgeGo;
  13. GameObject shieldGo;
  14. Text shieldNumText;
  15. RectTransform rt = null;
  16. Animator mAnim = null;
  17. float beginShowTime = 0;
  18. float duration = 1.0f;
  19. long loadSeq = 0;
  20. public bool Finished
  21. {
  22. get
  23. {
  24. if (target == null) return true;
  25. float passedTime = Time.realtimeSinceStartup - beginShowTime;
  26. if (passedTime >= duration)
  27. return true;
  28. return false;
  29. }
  30. }
  31. void Start()
  32. {
  33. }
  34. void Update()
  35. {
  36. //if (bDisposed || !bInited) return;
  37. //UpdatePos();
  38. }
  39. private void OnDestroy()
  40. {
  41. Dispose();
  42. }
  43. public void ShowBuffText(Fighter fighter, string icon,bool upAnim)
  44. {
  45. OnShow(fighter, upAnim);
  46. missGo.SetActive(false);
  47. shieldGo.gameObject.SetActive(false);
  48. dodgeGo.SetActive(false);
  49. if (functionImg != null)
  50. {
  51. loadSeq = ResourceMgr.Instance.LoadAsset<Sprite>(OnLoadBuffTextCompleted, Constants.IconDir, icon);
  52. }
  53. }
  54. void OnLoadBuffTextCompleted(Sprite sp, string assetPath, params string[] assetNames)
  55. {
  56. loadSeq = 0;
  57. if(functionImg!=null)
  58. {
  59. functionImg.sprite = sp;
  60. functionImg.gameObject.SetActive(true);
  61. }
  62. }
  63. public void ShowShield(Fighter fighter, float hurt)
  64. {
  65. OnShow(fighter);
  66. missGo.SetActive(false);
  67. dodgeGo.SetActive(false);
  68. functionImg.gameObject.SetActive(false);
  69. shieldGo.gameObject.SetActive(true);
  70. shieldNumText.text = I18N.T("Shield") + hurt.ToString();
  71. if(fighter.IsTeamMember)
  72. shieldNumText.color = Color.red;
  73. else
  74. shieldNumText.color = Color.white;
  75. }
  76. public void ShowMissing(Fighter fighter)
  77. {
  78. OnShow(fighter);
  79. missGo.SetActive(fighter.TeamSide == eTeamType.Enemy);
  80. dodgeGo.SetActive(fighter.TeamSide == eTeamType.Friend);
  81. functionImg.gameObject.SetActive(false);
  82. shieldGo.gameObject.SetActive(false);
  83. }
  84. public void Dispose()
  85. {
  86. if(loadSeq > 0)
  87. {
  88. ResourceMgr.Instance.UnloadAssetBySeqId(loadSeq);
  89. loadSeq = 0;
  90. }
  91. target = null;
  92. bDisposed = true;
  93. if (missGo) {
  94. missGo.SetActive(false);
  95. }
  96. if (dodgeGo) {
  97. dodgeGo.SetActive(false);
  98. }
  99. if (functionImg) {
  100. functionImg.gameObject.SetActive(false);
  101. }
  102. if (shieldGo) {
  103. shieldGo.gameObject.SetActive(false);
  104. }
  105. }
  106. void OnShow(Fighter fighter, bool upAnim = true)
  107. {
  108. if (!bInited)
  109. {
  110. InitCom();
  111. }
  112. target = fighter;
  113. beginShowTime = Time.realtimeSinceStartup;
  114. UpdatePos();
  115. if(gameObject.activeInHierarchy)
  116. {
  117. if (upAnim)
  118. {
  119. mAnim.Play("EffectUp");
  120. }
  121. else
  122. {
  123. mAnim.Play("EffectDown");
  124. }
  125. }
  126. }
  127. void UpdatePos()
  128. {
  129. if (target == null) return;
  130. Vector3 relativePos = target.Ctrl.GetUIPointPos() + target.Actor.AvatarData.buffTextPos;
  131. Vector3 pos = BattleCamera.Instance.RealCamera.WorldToViewportPoint(relativePos);
  132. if (rt != null)
  133. {
  134. rt.anchoredPosition3D = new Vector3((pos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (pos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
  135. }
  136. }
  137. void InitCom()
  138. {
  139. if (bInited) return;
  140. functionImg = item.Find("effect/function").GetComponent<Image>();
  141. missGo = item.Find("effect/MISS").gameObject;
  142. dodgeGo = item.Find("effect/DODGE").gameObject;
  143. shieldGo = item.Find("effect/Shield").gameObject;
  144. shieldNumText = item.Find("effect/Shield/hurt").GetComponent<Text>();
  145. rt = GetComponent<RectTransform>();
  146. mAnim = GetComponent<Animator>();
  147. bInited = true;
  148. }
  149. }