SmartLabelItem.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. using DG.Tweening;
  9. namespace UnityCommon
  10. {
  11. public class SmartLabelItem : MonoBehaviour
  12. {
  13. private Transform tfLine;
  14. private void Awake()
  15. {
  16. tfLine = transform.Find("Line");
  17. (tfLine as RectTransform).sizeDelta = new Vector2(offset, (tfLine as RectTransform).sizeDelta.y);
  18. Hide();
  19. }
  20. [HideInInspector]
  21. public Transform tfTargetItem;
  22. //private Vector3 centerP;
  23. private Canvas canvas;
  24. private float offset = 120f;
  25. private float angle = 0;
  26. public void SetLabel(string str, Canvas canvas, Transform tfTargetItem, float angle /*Vector3 centerP*/)
  27. {
  28. var txt = GetComponentInChildren<TextMeshProUGUI>();
  29. txt.text = str;
  30. var uilistener = UIEventListener.GetListener(transform.Find("Label").GetComponent<Image>().gameObject);
  31. uilistener.OnEnter = OnEnter;
  32. uilistener.OnExit = OnExit;
  33. uilistener.OnClick = OnClick;
  34. this.tfTargetItem = tfTargetItem;
  35. //this.centerP = centerP;
  36. this.angle = angle;
  37. this.canvas = canvas;
  38. }
  39. public void SetOffset(float offset)
  40. {
  41. this.offset = offset;
  42. }
  43. private void Update()
  44. {
  45. if (canvas != null && isShow)
  46. SetPosition();
  47. }
  48. private void SetPosition()
  49. {
  50. if (Camera.main.IsInFrontOfMe(tfTargetItem))
  51. {
  52. gameObject.SetActive(true);
  53. gameObject.transform.localScale = Vector3.one;
  54. var t = Utility.WordPosition2UILocalposition(Camera.main, canvas, tfTargetItem.position);
  55. //var c = Utility.WordPosition2UILocalposition(Camera.main, canvas, centerP);
  56. //var dir = (t - c).normalized;
  57. var dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.up;
  58. //dir = Quaternion.Inverse(dir);
  59. var labelP = t + dir.normalized * offset;
  60. (transform as RectTransform).anchoredPosition = labelP;
  61. if (tfLine != null)
  62. {
  63. //(tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
  64. (tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
  65. }
  66. }
  67. else
  68. {
  69. gameObject.transform.localScale = Vector3.zero;
  70. }
  71. }
  72. public void SetAngle(float angle)
  73. {
  74. this.angle = angle;
  75. }
  76. public void SetLabel(string label)
  77. {
  78. var txt = GetComponentInChildren<TextMeshProUGUI>();
  79. txt.text = label;
  80. }
  81. private SmartLabelHelperBase helper;
  82. public void SetHelper(SmartLabelHelperBase helper)
  83. {
  84. this.helper = helper;
  85. }
  86. private void OnClick(GameObject go, PointerEventData pEventData)
  87. {
  88. helper?.OnClick(this);
  89. }
  90. private void OnExit(GameObject go, PointerEventData pEventData)
  91. {
  92. helper?.OnExit(this);
  93. }
  94. private void OnEnter(GameObject go, PointerEventData pEventData)
  95. {
  96. transform.SetAsLastSibling();
  97. helper?.OnEnter(this);
  98. }
  99. private bool isShow = false;
  100. public void Show()
  101. {
  102. var t = Utility.WordPosition2UILocalposition(Camera.main, canvas, tfTargetItem.position);
  103. //var c = Utility.WordPosition2UILocalposition(Camera.main, canvas, centerP);
  104. //var dir = (t - c).normalized;
  105. var dir = Quaternion.AngleAxis(angle, Vector3.up).eulerAngles.normalized;
  106. var labelP = t + dir * offset;
  107. var rectTF = transform as RectTransform;
  108. rectTF.anchoredPosition = t;
  109. if (tfLine != null)
  110. {
  111. tfLine.localScale = new Vector3(0, 1, 1);
  112. (tfLine as RectTransform).rotation = Quaternion.FromToRotation(Vector3.right, -dir);
  113. }
  114. gameObject.SetActive(true);
  115. rectTF.DOLocalMove(labelP, 1).onComplete = () =>
  116. {
  117. isShow = true;
  118. };
  119. tfLine.DOScale(Vector3.one, 1);
  120. }
  121. public void Hide()
  122. {
  123. isShow = false;
  124. gameObject.SetActive(false);
  125. }
  126. }
  127. }