12using System.Collections.Generic;
53 public Func<Collider, T>
filter =
null;
55 private HashSet<T> _activeObjects =
new HashSet<T>();
66 private HashSet<T> _activeObjectsLastFrame =
new HashSet<T>();
67 private HashSet<T> _beganActiveObjects =
new HashSet<T>();
73 get {
return _beganActiveObjects; }
75 private HashSet<T> _endedActiveObjects =
new HashSet<T>();
81 get {
return _endedActiveObjects; }
90 private static Collider[] s_colliderResultsBuffer =
new Collider[32];
94 _activeObjects.Clear();
97 if (s_colliderResultsBuffer ==
null || s_colliderResultsBuffer.Length < 32) {
98 s_colliderResultsBuffer =
new Collider[32];
104 if (queryPosition.HasValue) {
105 Vector3 actualQueryPosition = queryPosition.GetValueOrDefault();
107 int count = GetSphereColliderResults(actualQueryPosition, ref s_colliderResultsBuffer);
108 UpdateActiveList(count, s_colliderResultsBuffer);
111 if (spaces !=
null) {
114 count = GetSphereColliderResults(transformPoint(actualQueryPosition, space), ref s_colliderResultsBuffer);
115 UpdateActiveList(count, s_colliderResultsBuffer);
123 _endedActiveObjects.Clear();
124 _beganActiveObjects.Clear();
126 foreach (var behaviour
in _activeObjects) {
127 if (!_activeObjectsLastFrame.Contains(behaviour)) {
128 _beganActiveObjects.Add(behaviour);
132 foreach (var behaviour
in _activeObjectsLastFrame) {
133 if (!_activeObjects.Contains(behaviour)) {
134 _endedActiveObjects.Add(behaviour);
138 _activeObjectsLastFrame.Clear();
139 foreach (var behaviour
in _activeObjects) {
140 _activeObjectsLastFrame.Add(behaviour);
147 private int GetSphereColliderResults(Vector3 position, ref Collider[] resultsBuffer) {
151 int overlapCount = 0;
153 overlapCount =
Physics.OverlapSphereNonAlloc(position,
157 QueryTriggerInteraction.Collide);
158 if (overlapCount < resultsBuffer.Length) {
165 resultsBuffer =
new Collider[resultsBuffer.Length * 2];
172 private void UpdateActiveList(
int numResults, Collider[] results) {
174 Debug.LogWarning(
"[ActivityManager] No filter provided for this ActivityManager; no objects will ever be returned."
175 +
"Please make sure you specify a non-null filter property of any ActivityManagers you wish to use.");
179 for (
int i = 0; i < numResults; i++) {
180 T obj =
filter(results[i]);
182 _activeObjects.Add(obj);
188 Vector3 localPalmPos = space.transform.InverseTransformPoint(worldPoint);
ActivityManager is a wrapper around PhysX sphere queries for arbitrary Unity objects....
bool trackStateChanges
If set to true, BeganActive and EndedActive will be calculated and populated every time a new query o...
ActivityManager(float activationRadius, Func< Collider, T > filter)
void UpdateActivityQuery(Vector3? queryPosition, List< LeapSpace > spaces=null)
Func< int > activationLayerFunction
This function, if set to a non-null value, overrides the activationLayerMask setting with the result ...
HashSet< T > ActiveObjects
Returns the currently "active" objects – objects that were within the latest sphere query.
HashSet< T > EndedActive
If trackStateChanges is enabled (on by default), contains the objects that just stopped being active ...
int activationLayerMask
The layer mask against which to query for active objects. The ActivityManager will only find objects ...
HashSet< T > BeganActive
If trackStateChanges is enabled (on by default), contains the objects that just started being active ...
Func< Collider, T > filter
This is the function by which the ActivityManager converts the Colliders it finds through PhysX queri...
float activationRadius
The radius of the query in world-space.
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....