ProximityDetector.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Leap.Unity
  6. {
  7. public class ProximityDetector : Detector
  8. {
  9. public GameObject CurrentObject
  10. {
  11. get
  12. {
  13. return this._currentObj;
  14. }
  15. }
  16. private void Awake()
  17. {
  18. this.proximityWatcherCoroutine = this.proximityWatcher();
  19. if (this.TagName != string.Empty)
  20. {
  21. GameObject[] array = GameObject.FindGameObjectsWithTag(this.TagName);
  22. List<GameObject> list = new List<GameObject>(array.Length + this.TargetObjects.Length);
  23. for (int i = 0; i < this.TargetObjects.Length; i++)
  24. {
  25. list.Add(this.TargetObjects[i]);
  26. }
  27. for (int j = 0; j < array.Length; j++)
  28. {
  29. list.Add(array[j]);
  30. }
  31. this.TargetObjects = list.ToArray();
  32. }
  33. }
  34. private void OnEnable()
  35. {
  36. base.StopCoroutine(this.proximityWatcherCoroutine);
  37. base.StartCoroutine(this.proximityWatcherCoroutine);
  38. }
  39. private void OnDisable()
  40. {
  41. base.StopCoroutine(this.proximityWatcherCoroutine);
  42. this.Deactivate();
  43. }
  44. private IEnumerator proximityWatcher()
  45. {
  46. bool proximityState = false;
  47. for (;;)
  48. {
  49. float onSquared = this.OnDistance * this.OnDistance;
  50. float offSquared = this.OffDistance * this.OffDistance;
  51. if (this._currentObj != null)
  52. {
  53. if (this.distanceSquared(this._currentObj) > offSquared)
  54. {
  55. this._currentObj = null;
  56. proximityState = false;
  57. }
  58. }
  59. else if (this.UseLayersNotList)
  60. {
  61. Collider[] array = Physics.OverlapSphere(base.transform.position, this.OnDistance, this.Layer);
  62. if (array.Length > 0)
  63. {
  64. this._currentObj = array[0].gameObject;
  65. proximityState = true;
  66. this.OnProximity.Invoke(this._currentObj);
  67. }
  68. }
  69. else
  70. {
  71. for (int i = 0; i < this.TargetObjects.Length; i++)
  72. {
  73. GameObject gameObject = this.TargetObjects[i];
  74. if (this.distanceSquared(gameObject) < onSquared)
  75. {
  76. this._currentObj = gameObject;
  77. proximityState = true;
  78. this.OnProximity.Invoke(this._currentObj);
  79. break;
  80. }
  81. }
  82. }
  83. if (proximityState)
  84. {
  85. this.Activate();
  86. }
  87. else
  88. {
  89. this.Deactivate();
  90. }
  91. yield return new WaitForSeconds(this.Period);
  92. }
  93. yield break;
  94. }
  95. private float distanceSquared(GameObject target)
  96. {
  97. Collider component = target.GetComponent<Collider>();
  98. Vector3 a;
  99. if (component != null)
  100. {
  101. a = component.ClosestPointOnBounds(base.transform.position);
  102. }
  103. else
  104. {
  105. a = target.transform.position;
  106. }
  107. return (a - base.transform.position).sqrMagnitude;
  108. }
  109. [Tooltip("The interval in seconds at which to check this detector's conditions.")]
  110. public float Period = 0.1f;
  111. [Tooltip("Dispatched when close enough to a target.")]
  112. public ProximityEvent OnProximity;
  113. [Tooltip("The list of target objects.")]
  114. public GameObject[] TargetObjects;
  115. [Tooltip("Objects with this tag are added to the list of targets.")]
  116. public string TagName = string.Empty;
  117. [Tooltip("Use a Layer instead of the target list.")]
  118. public bool UseLayersNotList;
  119. [Tooltip("The Layer containing the objects to check.")]
  120. public LayerMask Layer;
  121. [Tooltip("The target distance in meters to activate the detector.")]
  122. public float OnDistance = 0.01f;
  123. [Tooltip("The distance in meters at which to deactivate the detector.")]
  124. public float OffDistance = 0.015f;
  125. private IEnumerator proximityWatcherCoroutine;
  126. private GameObject _currentObj;
  127. }
  128. }