123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Leap.Unity
- {
- public class ProximityDetector : Detector
- {
- public GameObject CurrentObject
- {
- get
- {
- return this._currentObj;
- }
- }
- private void Awake()
- {
- this.proximityWatcherCoroutine = this.proximityWatcher();
- if (this.TagName != string.Empty)
- {
- GameObject[] array = GameObject.FindGameObjectsWithTag(this.TagName);
- List<GameObject> list = new List<GameObject>(array.Length + this.TargetObjects.Length);
- for (int i = 0; i < this.TargetObjects.Length; i++)
- {
- list.Add(this.TargetObjects[i]);
- }
- for (int j = 0; j < array.Length; j++)
- {
- list.Add(array[j]);
- }
- this.TargetObjects = list.ToArray();
- }
- }
- private void OnEnable()
- {
- base.StopCoroutine(this.proximityWatcherCoroutine);
- base.StartCoroutine(this.proximityWatcherCoroutine);
- }
- private void OnDisable()
- {
- base.StopCoroutine(this.proximityWatcherCoroutine);
- this.Deactivate();
- }
- private IEnumerator proximityWatcher()
- {
- bool proximityState = false;
- for (;;)
- {
- float onSquared = this.OnDistance * this.OnDistance;
- float offSquared = this.OffDistance * this.OffDistance;
- if (this._currentObj != null)
- {
- if (this.distanceSquared(this._currentObj) > offSquared)
- {
- this._currentObj = null;
- proximityState = false;
- }
- }
- else if (this.UseLayersNotList)
- {
- Collider[] array = Physics.OverlapSphere(base.transform.position, this.OnDistance, this.Layer);
- if (array.Length > 0)
- {
- this._currentObj = array[0].gameObject;
- proximityState = true;
- this.OnProximity.Invoke(this._currentObj);
- }
- }
- else
- {
- for (int i = 0; i < this.TargetObjects.Length; i++)
- {
- GameObject gameObject = this.TargetObjects[i];
- if (this.distanceSquared(gameObject) < onSquared)
- {
- this._currentObj = gameObject;
- proximityState = true;
- this.OnProximity.Invoke(this._currentObj);
- break;
- }
- }
- }
- if (proximityState)
- {
- this.Activate();
- }
- else
- {
- this.Deactivate();
- }
- yield return new WaitForSeconds(this.Period);
- }
- yield break;
- }
- private float distanceSquared(GameObject target)
- {
- Collider component = target.GetComponent<Collider>();
- Vector3 a;
- if (component != null)
- {
- a = component.ClosestPointOnBounds(base.transform.position);
- }
- else
- {
- a = target.transform.position;
- }
- return (a - base.transform.position).sqrMagnitude;
- }
- [Tooltip("The interval in seconds at which to check this detector's conditions.")]
- public float Period = 0.1f;
- [Tooltip("Dispatched when close enough to a target.")]
- public ProximityEvent OnProximity;
- [Tooltip("The list of target objects.")]
- public GameObject[] TargetObjects;
- [Tooltip("Objects with this tag are added to the list of targets.")]
- public string TagName = string.Empty;
- [Tooltip("Use a Layer instead of the target list.")]
- public bool UseLayersNotList;
- [Tooltip("The Layer containing the objects to check.")]
- public LayerMask Layer;
- [Tooltip("The target distance in meters to activate the detector.")]
- public float OnDistance = 0.01f;
- [Tooltip("The distance in meters at which to deactivate the detector.")]
- public float OffDistance = 0.015f;
- private IEnumerator proximityWatcherCoroutine;
- private GameObject _currentObj;
- }
- }
|