123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class Arrow : MonoBehaviour {
- [SerializeField] private Vector2 m_ActiveDistanceRange = new Vector2(1.0f, 5.0f);
- [SerializeField] private bool ifDebugLog = false;
- private Renderer[] m_Renderers = null;
- private float m_Distance = 0;
- private Transform m_Trans = null;
- [SerializeField] private Transform m_TransCamera = null;
- private void Awake() {
- m_Renderers = gameObject.GetComponentsInChildren<Renderer>();
- m_Trans = transform;
- m_Renderers.ToList().ForEach((mr) => { mr.enabled = false; });
- }
- private void OnEnable() {
- StartCoroutine(EnumCheckDistance());
- }
- private void OnDisable() {
- StopAllCoroutines();
- }
- IEnumerator EnumCheckDistance() {
- while (true) {
- yield return new WaitForSeconds(1.0f);
- m_Distance = Vector3.Distance(m_Trans.position, m_TransCamera.position);
- if (ifDebugLog) {
- Debug.LogFormat("\"{0}\" 距离:{1}", name, m_Distance);
- }
- if (m_Distance >= m_ActiveDistanceRange.x && m_Distance <= m_ActiveDistanceRange.y) {
- Active();
- } else {
- Deactive();
- }
- }
- }
- void Active() {
- m_Renderers.ToList().ForEach((mr) => { mr.enabled = true; });
- }
- void Deactive() {
- m_Renderers.ToList().ForEach((mr) => { mr.enabled = false; });
- }
- }
|