JianTouManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Sirenix.OdinInspector;
  2. using UnityEngine;
  3. public class JianTouManager : MonoBehaviour
  4. {
  5. public static JianTouManager instance;
  6. [ReadOnly]
  7. [LabelText("是否激活")]
  8. public bool active = true;
  9. [LabelText("是否开启近小远大")]
  10. public bool isScale = false;
  11. public static JianTouManager Instance
  12. {
  13. get
  14. {
  15. if (instance == null)
  16. {
  17. instance = FindObjectOfType<JianTouManager>();
  18. if (instance == null)
  19. {
  20. GameObject prefab = Resources.Load<GameObject>("箭头");
  21. GameObject jianTou = Instantiate(prefab,null);
  22. instance = jianTou.GetComponent<JianTouManager>();
  23. }
  24. }
  25. return instance;
  26. }
  27. }
  28. private float _minDistance = 0.5f;
  29. private float _maxDistance = 10f;
  30. private float _minScale = 0.5f;
  31. private float _maxScale = 2;
  32. private Animation _animation;
  33. private GameObject _jiantou;
  34. private float _distance;
  35. private Vector3 _scale;
  36. private Bounds _bound;
  37. private Transform currentHitTra;
  38. private void Awake()
  39. {
  40. if (instance == null)
  41. {
  42. instance = this;
  43. }
  44. _animation = this.transform.GetChild(0).GetComponent<Animation>();
  45. _jiantou = this.gameObject;
  46. }
  47. void Update()
  48. {
  49. if (isScale)
  50. {
  51. if (_jiantou.activeSelf)
  52. {
  53. _distance = Vector3.Distance(_jiantou.transform.position, Camera.main.transform.position);
  54. if (_distance < _minDistance)
  55. {
  56. _scale = Vector3.one * _minScale;
  57. }
  58. else
  59. {
  60. _scale = Vector3.one * Mathf.Lerp(_minScale, _maxScale, _distance / _maxDistance);
  61. }
  62. _jiantou.transform.localScale = Vector3.Lerp(_jiantou.transform.localScale, _scale, Time.deltaTime * 10f);
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// 箭头设置
  68. /// </summary>
  69. /// <param name="hightLightScr"></param>
  70. /// <param name="t"></param>
  71. public void ShowJianTou(Transform t)
  72. {
  73. currentHitTra = t;
  74. _bound = GetBounds(t);
  75. _animation.enabled = true;
  76. _jiantou.SetActive(true);
  77. _minScale = Mathf.Clamp(((_bound.size.x + _bound.size.y + _bound.size.z) / 3), 0.05f, 0.5f);
  78. if (isScale)
  79. _jiantou.transform.localScale = Vector3.one * _minScale;
  80. _jiantou.transform.position = t.position + Vector3.up * ((_bound.size.y * 0.6f) < 0.06f ? 0.06f : (_bound.size.y * 0.6f));
  81. _animation.Play("Jump2");
  82. }
  83. public void ShowJianTou(Vector3 point, float hight)
  84. {
  85. _animation.enabled = true;
  86. this.gameObject.SetActive(true);
  87. //HighlightingManager.Instance.OnLighting(_jiantou.gameObject);
  88. this.transform.position = point + Vector3.up * hight;
  89. _animation.Play("Take 001");
  90. }
  91. public void HideJianTou()
  92. {
  93. _animation.enabled = false;
  94. this.gameObject.SetActive(false);
  95. }
  96. public void HideJianTou(Transform tra)
  97. {
  98. Debug.LogError(currentHitTra == tra);
  99. if (currentHitTra == tra)
  100. {
  101. _animation.enabled = false;
  102. this.gameObject.SetActive(false);
  103. }
  104. }
  105. public void SetJianTouActive(bool _active)
  106. {
  107. active = _active;
  108. foreach (var item in this.GetComponentsInChildren<MeshRenderer>())
  109. {
  110. item.enabled = _active;
  111. }
  112. }
  113. /// <summary>
  114. /// 获取Transform及其子级的包围盒(MeshRenderer),若MeshRenderer包围盒长度为0,则获取BoxCollider包围盒
  115. /// </summary>
  116. /// <param name="transform"></param>
  117. /// <returns></returns>
  118. private static Bounds GetBounds(Transform transform)
  119. {
  120. Quaternion oldRotation = Quaternion.identity;
  121. bool boundsInitialized = true;
  122. Bounds bounds = new Bounds(transform.position, Vector3.zero);
  123. Renderer[] renderers = transform.GetComponentsInChildren<Renderer>();
  124. foreach (Renderer renderer in renderers)
  125. {
  126. if (!boundsInitialized)
  127. {
  128. bounds = new Bounds(renderer.transform.position, Vector3.zero);
  129. boundsInitialized = true;
  130. }
  131. bounds.Encapsulate(renderer.bounds);
  132. }
  133. if (bounds.size.magnitude == 0)
  134. {
  135. BoxCollider[] colliders = transform.GetComponentsInChildren<BoxCollider>();
  136. foreach (BoxCollider collider in colliders)
  137. {
  138. if (!boundsInitialized)
  139. {
  140. bounds = new Bounds(collider.transform.position, Vector3.zero);
  141. boundsInitialized = true;
  142. }
  143. bounds.Encapsulate(collider.bounds);
  144. }
  145. }
  146. return bounds;
  147. }
  148. }