#if UNITY_EDITOR
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.XR.Interaction.Toolkit.Utilities;
namespace UnityEditor.XR.Interaction.Toolkit.Utilities
{
///
/// Editor utility methods for locating Component instances.
///
///
static class EditorComponentLocatorUtility
{
///
/// Returns the first active loaded object of the given type in the same Scene as the GameObject,
/// biasing towards being hierarchically related to the GameObject.
///
/// The Component type to find.
/// The in the Scene to search.
///
/// Returns the object that matches the specified type in the Scene.
/// Otherwise, returns if no object matches the type in the Scene.
///
///
/// This method can be used when finding a Component to reference in the same Scene
/// since serialization of cross scene references are not supported.
///
public static T FindSceneComponentOfType(GameObject gameObject) where T : Component
{
if (gameObject == null)
return null;
// 1. Search children first since those can be serialized with a prefab.
// 2. Search parents for logical ownership.
// 3. Search the rest of the Scene.
var component = gameObject.GetComponentInChildren(true);
if (component != null)
return component;
component = gameObject.GetComponentInParent();
if (component != null)
return component;
return FindSceneComponentOfType(gameObject.scene);
}
///
/// Returns the first active loaded object of the given type in the same Scene.
///
/// The Component type to find.
/// The to search.
///
/// Returns the object that matches the specified type in the Scene.
/// Otherwise, returns if no object matches the type in the Scene.
///
///
/// This method can be used when finding a Component to reference in the same Scene
/// since serialization of cross scene references are not supported.
///
public static T FindSceneComponentOfType(Scene scene) where T : Component
{
var currentStage = StageUtility.GetCurrentStageHandle();
var components = currentStage.FindComponentsOfType();
foreach (var component in components)
{
if (component.gameObject.scene == scene)
{
return component;
}
}
return null;
}
}
}
#endif