namespace UnityEngine.XR.Interaction.Toolkit.Utilities
{
///
/// Utility methods for locating component instances.
///
/// The component type.
static class ComponentLocatorUtility where T : Component
{
///
/// Cached reference to a found component of type .
///
static T s_ComponentCache;
///
/// Cached reference to a found component of type .
///
internal static T componentCache => s_ComponentCache;
///
/// Last frame that was called.
///
static int s_LastTryFindFrame = -1;
static bool FindWasPerformedThisFrame() => s_LastTryFindFrame == Time.frameCount;
///
/// Find or create a new GameObject with component .
///
/// Returns the found or created component.
///
/// Does not include inactive GameObjects when finding the component, but if a component was previously created
/// as a direct result of this class, it will return that component even if the GameObject is now inactive.
///
public static T FindOrCreateComponent()
{
if (s_ComponentCache == null)
{
s_ComponentCache = Find();
if (s_ComponentCache == null)
s_ComponentCache = new GameObject(typeof(T).Name, typeof(T)).GetComponent();
}
return s_ComponentCache;
}
///
/// Find a component .
///
/// Returns the found component, or if one could not be found.
///
/// Does not include inactive GameObjects when finding the component, but if a component was previously created
/// as a direct result of this class, it will return that component even if the GameObject is now inactive.
///
public static T FindComponent()
{
TryFindComponent(out var component);
return component;
}
///
/// Find a component .
///
/// When this method returns, contains the found component, or if one could not be found.
/// Returns if the component exists, otherwise returns .
///
/// Does not include inactive GameObjects when finding the component, but if a component was previously created
/// as a direct result of this class, it will return that component even if the GameObject is now inactive.
///
///
public static bool TryFindComponent(out T component)
{
if (s_ComponentCache != null)
{
component = s_ComponentCache;
return true;
}
s_ComponentCache = Find();
component = s_ComponentCache;
return component != null;
}
///
/// Find a component .
///
/// When this method returns, contains the found component, or if one could not be found.
/// If , this method will only perform if it has not already been unsuccessfully called this frame.
/// Returns if the component exists, otherwise returns .
/// This function will return a cached component from a previous search regardless if is .
internal static bool TryFindComponent(out T component, bool limitTryFindPerFrame)
{
// If a search for this component has already been unsuccessfully performed this frame, don't search again.
if (limitTryFindPerFrame && FindWasPerformedThisFrame() && s_ComponentCache == null)
{
component = null;
return false;
}
return TryFindComponent(out component);
}
static T Find()
{
s_LastTryFindFrame = Time.frameCount;
// Prior to Unity 6.4, we are sorting by ID since this utility is often used to find a component of which there is a single instance,
// so the penalty for sorting will be minimal but with the benefit of having consistent results when
// there are multiple components to choose from. With Unity 6.4+, sort order cannot be maintained when
// changing from instance ID to entity ID, so FindObjectsSortMode was deprecated. Rather than suppressing the warning,
// we just use the equivalent of FindObjectsSortMode.None.
#if UNITY_6000_4_OR_NEWER
var objectsByType = Object.FindObjectsByType(typeof(T), FindObjectsInactive.Exclude);
#else
var objectsByType = Object.FindObjectsByType(typeof(T), FindObjectsInactive.Exclude, FindObjectsSortMode.InstanceID);
#endif
if (objectsByType.Length == 0)
return null;
if (objectsByType.Length > 1)
{
// In the case of multiple components, prioritize the first that is enabled
for (var i = 0; i < objectsByType.Length; ++i)
{
var obj = objectsByType[i];
if (obj is Behaviour behavior && behavior.enabled)
return (T)obj;
}
}
return (T)objectsByType[0];
}
}
}