Search Unity

Question How to "Find Reference in Scene" in the herarchy using C#

Discussion in 'Scripting' started by AlanMattano, Aug 4, 2020.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I was trying to imitate "Find References 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.

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System;
    6. using UnityEngine.SceneManagement;
    7.  
    8. // Search and select one game object that contains the selected component,
    9.  
    10. public class FindScript: MonoBehaviour
    11. {
    12.  
    13.     [MenuItem("Assets/Find Script in Scene")]
    14.     private static void FindInScene()
    15.     {
    16.         var scriptSelected = Selection.activeObject;
    17.  
    18.         string selectedScriptName = scriptSelected.name;
    19.         //Debug.Log("NAME:\n" + selectedScriptName);
    20.  
    21.         string selectedAssetName = AssetDatabase.GetAssetPath(scriptSelected);
    22.  
    23.         Type scriptType = scriptSelected.GetType();
    24.         //Debug.Log("PATH:\n" + selectedScriptName);
    25.  
    26.         // Find the game object that includes the script
    27.        
    28.         //Resources.FindObjectsOfTypeAll(typeof(scriptType));
    29.  
    30.         //Scene scene = MonoBehaviour.SceneManager.GetActiveScene();
    31.  
    32.         /*
    33.         Scene myScene = SceneManager.GetActiveScene();
    34.  
    35.         GameObject[] gameObjects = myScene.GetRootGameObjects();
    36.  
    37.         foreach (GameObject acutalObject in gameObjects)
    38.         {
    39.             foreach (Component comp in acutalObject.GetComponents(scriptType))
    40.             {
    41.                 if (comp.name == selectedScriptName || comp.name == selectedAssetName)
    42.                 {
    43.                     Selection.activeGameObject = acutalObject.gameObject;
    44.                 }
    45.             }
    46.         }*/
    47.  
    48.         /*
    49.         List<GameObject> objectsInScene = new List<GameObject>();
    50.         foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
    51.         {
    52.             if (EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
    53.             {
    54.                 //objectsInScene.Add(go);
    55.                 // all GameObjects in the scene
    56.  
    57.                 foreach (Component component in go.GetComponents(scriptType))
    58.                 {
    59.                     if (component.name == selectedScriptName || component.name == selectedAssetName)
    60.                     {
    61.                         Selection.activeGameObject = go.gameObject;
    62.                     }
    63.                 }              
    64.             }
    65.         }*/
    66.     }
    67. }
    68.  
     
  2. Ultimate_Cooper

    Ultimate_Cooper

    Joined:
    Jul 27, 2020
    Posts:
    8
    The best I can think of is the Get Child component.
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281