Arrow.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class Arrow : MonoBehaviour {
  6. [SerializeField] private Vector2 m_ActiveDistanceRange = new Vector2(1.0f, 5.0f);
  7. [SerializeField] private bool ifDebugLog = false;
  8. private Renderer[] m_Renderers = null;
  9. private float m_Distance = 0;
  10. private Transform m_Trans = null;
  11. [SerializeField] private Transform m_TransCamera = null;
  12. private void Awake() {
  13. m_Renderers = gameObject.GetComponentsInChildren<Renderer>();
  14. m_Trans = transform;
  15. m_Renderers.ToList().ForEach((mr) => { mr.enabled = false; });
  16. }
  17. private void OnEnable() {
  18. StartCoroutine(EnumCheckDistance());
  19. }
  20. private void OnDisable() {
  21. StopAllCoroutines();
  22. }
  23. IEnumerator EnumCheckDistance() {
  24. while (true) {
  25. yield return new WaitForSeconds(1.0f);
  26. m_Distance = Vector3.Distance(m_Trans.position, m_TransCamera.position);
  27. if (ifDebugLog) {
  28. Debug.LogFormat("\"{0}\" 距离:{1}", name, m_Distance);
  29. }
  30. if (m_Distance >= m_ActiveDistanceRange.x && m_Distance <= m_ActiveDistanceRange.y) {
  31. Active();
  32. } else {
  33. Deactive();
  34. }
  35. }
  36. }
  37. void Active() {
  38. m_Renderers.ToList().ForEach((mr) => { mr.enabled = true; });
  39. }
  40. void Deactive() {
  41. m_Renderers.ToList().ForEach((mr) => { mr.enabled = false; });
  42. }
  43. }