I was trying to imitate “Find Referen**ces** in Scene” and make a “Find Reference in Scene” when I right-click.
But I was incapable of selecting the game object in the hierarchy.
@karl_jones is there a way to get access to the Hierarchy search box via script?
for example “t:TestScript”
Here some tentatives.
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.SceneManagement;
// Search and select one game object that contains the selected component,
public class FindScript: MonoBehaviour
{
[MenuItem("Assets/Find Script in Scene")]
private static void FindInScene()
{
var scriptSelected = Selection.activeObject;
string selectedScriptName = scriptSelected.name;
//Debug.Log("NAME:\n" + selectedScriptName);
string selectedAssetName = AssetDatabase.GetAssetPath(scriptSelected);
Type scriptType = scriptSelected.GetType();
//Debug.Log("PATH:\n" + selectedScriptName);
// Find the game object that includes the script
//Resources.FindObjectsOfTypeAll(typeof(scriptType));
//Scene scene = MonoBehaviour.SceneManager.GetActiveScene();
/*
Scene myScene = SceneManager.GetActiveScene();
GameObject[] gameObjects = myScene.GetRootGameObjects();
foreach (GameObject acutalObject in gameObjects)
{
foreach (Component comp in acutalObject.GetComponents(scriptType))
{
if (comp.name == selectedScriptName || comp.name == selectedAssetName)
{
Selection.activeGameObject = acutalObject.gameObject;
}
}
}*/
/*
List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
if (EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
{
//objectsInScene.Add(go);
// all GameObjects in the scene
foreach (Component component in go.GetComponents(scriptType))
{
if (component.name == selectedScriptName || component.name == selectedAssetName)
{
Selection.activeGameObject = go.gameObject;
}
}
}
}*/
}
}