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(); if (instance == null) { GameObject prefab = Resources.Load("箭头"); GameObject jianTou = Instantiate(prefab,null); instance = jianTou.GetComponent(); } } 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(); _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); } } } /// /// 箭头设置 /// /// /// 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()) { item.enabled = _active; } } /// /// 获取Transform及其子级的包围盒(MeshRenderer),若MeshRenderer包围盒长度为0,则获取BoxCollider包围盒 /// /// /// 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(); 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(); foreach (BoxCollider collider in colliders) { if (!boundsInitialized) { bounds = new Bounds(collider.transform.position, Vector3.zero); boundsInitialized = true; } bounds.Encapsulate(collider.bounds); } } return bounds; } }