123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using Sirenix.OdinInspector;
- using UnityEngine;
- public class JianTouManager : MonoBehaviour
- {
- public static JianTouManager instance;
- [ReadOnly]
- [LabelText("是否激活")]
- public bool active = true;
- [LabelText("是否开启近小远大")]
- public bool isScale = false;
- public static JianTouManager Instance
- {
- get
- {
- if (instance == null)
- {
- instance = FindObjectOfType<JianTouManager>();
- if (instance == null)
- {
- GameObject prefab = Resources.Load<GameObject>("箭头");
- GameObject jianTou = Instantiate(prefab,null);
- instance = jianTou.GetComponent<JianTouManager>();
- }
- }
- return instance;
- }
- }
- private float _minDistance = 0.5f;
- private float _maxDistance = 10f;
- private float _minScale = 0.5f;
- private float _maxScale = 2;
- private Animation _animation;
- private GameObject _jiantou;
- private float _distance;
- private Vector3 _scale;
- private Bounds _bound;
- private Transform currentHitTra;
- private void Awake()
- {
- if (instance == null)
- {
- instance = this;
- }
- _animation = this.transform.GetChild(0).GetComponent<Animation>();
- _jiantou = this.gameObject;
- }
- void Update()
- {
- if (isScale)
- {
- if (_jiantou.activeSelf)
- {
- _distance = Vector3.Distance(_jiantou.transform.position, Camera.main.transform.position);
- if (_distance < _minDistance)
- {
- _scale = Vector3.one * _minScale;
- }
- else
- {
- _scale = Vector3.one * Mathf.Lerp(_minScale, _maxScale, _distance / _maxDistance);
- }
- _jiantou.transform.localScale = Vector3.Lerp(_jiantou.transform.localScale, _scale, Time.deltaTime * 10f);
- }
- }
- }
- /// <summary>
- /// 箭头设置
- /// </summary>
- /// <param name="hightLightScr"></param>
- /// <param name="t"></param>
- public void ShowJianTou(Transform t)
- {
- currentHitTra = t;
- _bound = GetBounds(t);
- _animation.enabled = true;
- _jiantou.SetActive(true);
- _minScale = Mathf.Clamp(((_bound.size.x + _bound.size.y + _bound.size.z) / 3), 0.05f, 0.5f);
- if (isScale)
- _jiantou.transform.localScale = Vector3.one * _minScale;
- _jiantou.transform.position = t.position + Vector3.up * ((_bound.size.y * 0.6f) < 0.06f ? 0.06f : (_bound.size.y * 0.6f));
- _animation.Play("Jump2");
-
- }
- public void ShowJianTou(Vector3 point, float hight)
- {
- _animation.enabled = true;
- this.gameObject.SetActive(true);
- //HighlightingManager.Instance.OnLighting(_jiantou.gameObject);
- this.transform.position = point + Vector3.up * hight;
- _animation.Play("Take 001");
- }
- public void HideJianTou()
- {
- _animation.enabled = false;
- this.gameObject.SetActive(false);
- }
- public void HideJianTou(Transform tra)
- {
- Debug.LogError(currentHitTra == tra);
- if (currentHitTra == tra)
- {
- _animation.enabled = false;
- this.gameObject.SetActive(false);
- }
- }
-
- public void SetJianTouActive(bool _active)
- {
- active = _active;
- foreach (var item in this.GetComponentsInChildren<MeshRenderer>())
- {
- item.enabled = _active;
- }
- }
- /// <summary>
- /// 获取Transform及其子级的包围盒(MeshRenderer),若MeshRenderer包围盒长度为0,则获取BoxCollider包围盒
- /// </summary>
- /// <param name="transform"></param>
- /// <returns></returns>
- private static Bounds GetBounds(Transform transform)
- {
- Quaternion oldRotation = Quaternion.identity;
- bool boundsInitialized = true;
- Bounds bounds = new Bounds(transform.position, Vector3.zero);
- Renderer[] renderers = transform.GetComponentsInChildren<Renderer>();
- foreach (Renderer renderer in renderers)
- {
- if (!boundsInitialized)
- {
- bounds = new Bounds(renderer.transform.position, Vector3.zero);
- boundsInitialized = true;
- }
- bounds.Encapsulate(renderer.bounds);
- }
- if (bounds.size.magnitude == 0)
- {
- BoxCollider[] colliders = transform.GetComponentsInChildren<BoxCollider>();
- foreach (BoxCollider collider in colliders)
- {
- if (!boundsInitialized)
- {
- bounds = new Bounds(collider.transform.position, Vector3.zero);
- boundsInitialized = true;
- }
- bounds.Encapsulate(collider.bounds);
- }
- }
- return bounds;
- }
- }
|