-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
243 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,6 @@ private void DisconnectEventRouteFromKnownInteractables(IXRInteractableEventRout | |
private bool IsValidChild(IXRInteractable interactable) | ||
{ | ||
return interactable is MonoBehaviour behaviour && | ||
behaviour.GetComponentInParent<InteractableEventRouter>() == this && | ||
behaviour.gameObject != gameObject; | ||
} | ||
|
||
|
@@ -453,12 +452,22 @@ public abstract class InteractableEventRoute<S, T> : IXRInteractableEventRoute | |
where S : IXRInteractable | ||
where T : IXRInteractableEventRouteTarget | ||
{ | ||
private T[] targets = null; | ||
private List<T> targets = null; | ||
|
||
/// <inheritdoc/> | ||
public void OnEnabled(GameObject origin) | ||
{ | ||
targets = GetTargets(origin); | ||
if (targets == null) | ||
{ | ||
targets = new List<T>(); | ||
} | ||
else | ||
{ | ||
targets.Clear(); | ||
} | ||
|
||
GetTargets(origin, targets); | ||
FilterTargets(origin, targets); | ||
} | ||
|
||
/// <inheritdoc/> | ||
|
@@ -472,7 +481,7 @@ public void Register(IXRInteractable interactable) | |
|
||
if (interactable is S source) | ||
{ | ||
for (int i = 0; i < targets.Length; i++) | ||
for (int i = 0; i < targets.Count; i++) | ||
{ | ||
if (targets[i] is T target) | ||
{ | ||
|
@@ -492,7 +501,7 @@ public void Unregister(IXRInteractable interactable) | |
|
||
if (interactable is S source) | ||
{ | ||
for (int i = 0; i < targets.Length; i++) | ||
for (int i = 0; i < targets.Count; i++) | ||
{ | ||
if (targets[i] is T target) | ||
{ | ||
|
@@ -502,14 +511,34 @@ public void Unregister(IXRInteractable interactable) | |
} | ||
} | ||
|
||
/// <summary> | ||
/// FIlter targets that are being targeted by other <see cref="InteractableEventRouter"/> objects. | ||
/// </summary> | ||
/// <param name="origin">The origin that is targetting <see cref="IXRInteractableEventRouteTarget"/>.</param> | ||
private void FilterTargets(GameObject origin, List<T> targets) | ||
{ | ||
for (int i = targets.Count - 1; i >= 0; i--) | ||
{ | ||
var target = targets[i]; | ||
if (target is MonoBehaviour behaviour) | ||
{ | ||
var router = behaviour.GetComponentInParent<InteractableEventRouter>(); | ||
if (router != null && router.gameObject != origin) | ||
{ | ||
targets.RemoveAt(i); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Search for and return all <see cref="IXRInteractableEventRouteTarget"/> objects that should receive events from the | ||
/// registered <see href="https://docs.unity3d.com/Packages/[email protected]/api/UnityEngine.XR.Interaction.Toolkit.IXRInteractable.html">IXRInteractable</see> | ||
/// objects. | ||
/// </summary> | ||
/// <param name="origin">This game object will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>.</param> | ||
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns> | ||
protected abstract T[] GetTargets(GameObject origin); | ||
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param> | ||
protected abstract void GetTargets(GameObject origin, List<T> targets); | ||
|
||
/// <summary> | ||
/// Add event handlers that will handle events from the <paramref name="source"/> and direct them to the <paramref name="target"/>. | ||
|
@@ -564,10 +593,10 @@ public abstract class InteractableParentEventRoute<S, T> : InteractableEventRout | |
/// <param name="origin"> | ||
/// The game object whose children will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>. | ||
/// </param> | ||
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns> | ||
protected override T[] GetTargets(GameObject origin) | ||
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param> | ||
protected override void GetTargets(GameObject origin, List<T> targets) | ||
{ | ||
return origin.GetComponentsInChildren<T>(includeInactive: true); | ||
origin.GetComponentsInChildren<T>(includeInactive: true, targets); | ||
} | ||
} | ||
|
||
|
@@ -602,10 +631,10 @@ public abstract class InteractableChildrenEventRoute<S, T> : InteractableEventRo | |
/// <param name="origin"> | ||
/// The game object whose children will be queried for components that implement <see cref="IXRInteractableEventRouteTarget"/>. | ||
/// </param> | ||
/// <returns>An array of specialized <see cref="IXRInteractableEventRouteTarget"/> components.</returns> | ||
protected override T[] GetTargets(GameObject origin) | ||
/// <param name="targets">A list that should be filled with specialized <see cref="IXRInteractableEventRouteTarget"/> component objects.</param> | ||
protected override void GetTargets(GameObject origin, List<T> targets) | ||
{ | ||
return origin.GetComponentsInChildren<T>(includeInactive: true); | ||
origin.GetComponentsInChildren<T>(includeInactive: true, targets); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.