DamageNumberGUI.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Serialization;
  5. using DamageNumbersPro.Internal;
  6. using TMPro;
  7. namespace DamageNumbersPro
  8. {
  9. [DisallowMultipleComponent]
  10. public class DamageNumberGUI : DamageNumber
  11. {
  12. /*
  13. * Contact me if you need any support.
  14. * Email: ekincantascontact@gmail.com
  15. * Discord: https:// discordcom/invite/nWbRkN8Zxr
  16. *
  17. * Check the manual for more information.
  18. * Manual: https:// ekincantascom/damage-numbers-pro/
  19. *
  20. * Thank you for using my asset.
  21. * If you want to add your own code please use the functions below.
  22. * I recommend creating a duplicate of this script first or creating your own script which derives from DamageNumber.
  23. * Otherwise you may loose your custom code when you update damage numbers pro.
  24. *
  25. * Good Luck
  26. */
  27. // Custom Events
  28. protected override void InternalOnPreSpawn()
  29. {
  30. // Fixes an issue where the previous mesh was visible for 1 frame
  31. if (textMeshProA != null)
  32. {
  33. textMeshProA.enabled = textMeshProB.enabled = false;
  34. }
  35. }
  36. protected override void InternalOnSpawn()
  37. {
  38. // Only damage numbers of the same parent can interact with each other
  39. if(spamGroup != "" && transform.parent != null)
  40. {
  41. spamGroup += transform.parent.GetInstanceID();
  42. }
  43. // GUI Alpha Fix
  44. skippedFrames = 0;
  45. skipFrames = true;
  46. realStartTime = Time.unscaledTime;
  47. }
  48. protected override void OnLateUpdate()
  49. {
  50. // GUI Alpha Fix
  51. if (skipFrames)
  52. {
  53. transform.localScale = Vector3.one * 0.0001f;
  54. currentFade = 0;
  55. if (skippedFrames > 2 && Time.unscaledTime > realStartTime + 0.03f)
  56. {
  57. skipFrames = false;
  58. transform.localScale = originalScale;
  59. currentFade = 0;
  60. }
  61. }
  62. }
  63. protected override void InternalUpdate(float deltaTime)
  64. {
  65. // GUI Alpha Fix
  66. skippedFrames++;
  67. }
  68. /*
  69. * The code below is required for the GUI version of damage numbers pro.
  70. * So you should not change it too much.
  71. * But you can use the events above to add your custom behavior.
  72. */
  73. // References
  74. RectTransform myRect;
  75. TextMeshProUGUI textMeshProA;
  76. TextMeshProUGUI textMeshProB;
  77. RectTransform textRectA;
  78. RectTransform textRectB;
  79. List<TMP_SubMeshUI> subMeshs;
  80. // Internal
  81. float realStartTime;
  82. bool skipFrames;
  83. int skippedFrames;
  84. // Components
  85. public override void GetReferencesIfNecessary()
  86. {
  87. if(textMeshProA == null)
  88. {
  89. GetReferences();
  90. }
  91. }
  92. public override void GetReferences()
  93. {
  94. baseAlpha = 0.9f;
  95. myRect = GetComponent<RectTransform>();
  96. transformA = transform.Find("TMPA");
  97. transformB = transform.Find("TMPB");
  98. textMeshProA = transformA.GetComponent<TextMeshProUGUI>();
  99. textMeshProB = transformB.GetComponent<TextMeshProUGUI>();
  100. textRectA = transformA.GetComponent<RectTransform>();
  101. textRectB = transformB.GetComponent<RectTransform>();
  102. }
  103. public override TMP_Text[] GetTextMeshs()
  104. {
  105. return new TMP_Text[] { textMeshProA, textMeshProB };
  106. }
  107. public override TMP_Text GetTextMesh()
  108. {
  109. return textMeshProA;
  110. }
  111. // Materials
  112. public override Material[] GetSharedMaterials()
  113. {
  114. return textMeshProA.fontSharedMaterials;
  115. }
  116. public override Material[] GetMaterials()
  117. {
  118. return textMeshProA.fontMaterials;
  119. }
  120. public override Material GetSharedMaterial()
  121. {
  122. return textMeshProA.fontSharedMaterial;
  123. }
  124. public override Material GetMaterial()
  125. {
  126. return textMeshProA.fontMaterial;
  127. }
  128. // Text
  129. protected override void SetTextString(string fullString)
  130. {
  131. textMeshProA.text = textMeshProB.text = fullString;
  132. if(!textMeshProA.enabled)
  133. {
  134. textMeshProA.enabled = textMeshProB.enabled = true;
  135. }
  136. textMeshProA.ForceMeshUpdate();
  137. textMeshProB.ForceMeshUpdate();
  138. textMeshProA.canvasRenderer.SetMesh(textMeshProA.mesh);
  139. textMeshProB.canvasRenderer.SetMesh(textMeshProB.mesh);
  140. meshs = new List<Mesh>();
  141. meshs.Add(textMeshProA.mesh);
  142. meshs.Add(textMeshProB.mesh);
  143. // Sub Meshs
  144. subMeshs = new List<TMP_SubMeshUI>();
  145. foreach(TMP_SubMeshUI subMesh in textMeshProA.GetComponentsInChildren<TMP_SubMeshUI>())
  146. {
  147. subMeshs.Add(subMesh);
  148. meshs.Add(subMesh.mesh);
  149. }
  150. foreach (TMP_SubMeshUI subMesh in textMeshProB.GetComponentsInChildren<TMP_SubMeshUI>())
  151. {
  152. subMeshs.Add(subMesh);
  153. meshs.Add(subMesh.mesh);
  154. }
  155. }
  156. // Position
  157. public override Vector3 GetPosition()
  158. {
  159. return myRect.anchoredPosition3D;
  160. }
  161. public override void SetPosition(Vector3 newPosition)
  162. {
  163. GetReferencesIfNecessary();
  164. position = myRect.anchoredPosition3D = newPosition;
  165. }
  166. protected override void SetFinalPosition(Vector3 newPosition)
  167. {
  168. GetReferencesIfNecessary();
  169. myRect.anchoredPosition3D = newPosition;
  170. }
  171. public override void SetAnchoredPosition(Transform rectParent, Vector2 anchoredPosition)
  172. {
  173. // Old Transform
  174. Vector3 oldScale = transform.localScale;
  175. // Set Parent and Position
  176. GetReferencesIfNecessary();
  177. myRect.SetParent(rectParent, false);
  178. myRect.anchoredPosition3D = anchoredPosition;
  179. // New Transform
  180. transform.localScale = oldScale;
  181. transform.eulerAngles = textMeshProA.canvas.transform.eulerAngles;
  182. }
  183. public override void SetAnchoredPosition(Transform rectParent, Transform rectPosition, Vector2 relativeAnchoredPosition)
  184. {
  185. // Old Transform
  186. Vector3 oldScale = transform.localScale;
  187. // Set Parent and Position
  188. GetReferencesIfNecessary();
  189. myRect.SetParent(rectParent, false);
  190. myRect.position = rectPosition.position;
  191. myRect.anchoredPosition += relativeAnchoredPosition;
  192. // New Transform
  193. transform.localScale = oldScale;
  194. transform.eulerAngles = textMeshProA.canvas.transform.eulerAngles;
  195. }
  196. protected override Vector3 GetOtherPosition(Transform target)
  197. {
  198. RectTransform targetRectTransform = followedTarget.GetComponent<RectTransform>();
  199. return targetRectTransform != null ? targetRectTransform.anchoredPosition3D : target.position;
  200. }
  201. protected override void SetLocalPositionA(Vector3 localPosition)
  202. {
  203. textRectA.anchoredPosition = localPosition * 50;
  204. }
  205. protected override void SetLocalPositionB(Vector3 localPosition)
  206. {
  207. textRectB.anchoredPosition = localPosition * 50;
  208. }
  209. protected override float GetPositionFactor()
  210. {
  211. return 100f;
  212. }
  213. // Other
  214. protected override void InternalUpdateFade(float currentFade)
  215. {
  216. textMeshProA.canvasRenderer.SetMesh(textMeshProA.mesh);
  217. textMeshProB.canvasRenderer.SetMesh(textMeshProB.mesh);
  218. foreach (TMP_SubMeshUI subMesh in subMeshs)
  219. {
  220. subMesh.canvasRenderer.SetMesh(subMesh.mesh);
  221. }
  222. }
  223. protected override void UpdateRotationZ()
  224. {
  225. SetRotationZ(textMeshProA.transform);
  226. SetRotationZ(textMeshProB.transform);
  227. }
  228. public override void CheckAndEnable3D()
  229. {
  230. enable3DGame = false;
  231. }
  232. public override bool IsMesh()
  233. {
  234. return false;
  235. }
  236. public override Vector3 GetUpVector()
  237. {
  238. return Vector3.up;
  239. }
  240. public override Vector3 GetRightVector()
  241. {
  242. return Vector3.right;
  243. }
  244. public override Vector3 GetFreshUpVector()
  245. {
  246. return Vector3.up;
  247. }
  248. public override Vector3 GetFreshRightVector()
  249. {
  250. return Vector3.right;
  251. }
  252. }
  253. }