Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How can I filter GameObjects in the Hierarchy including children ?

Discussion in 'Scripting' started by dubiduboni_unity, Mar 18, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    The problem is when I'm filtering now I'm getting results of the GameObjects but without the children of it.



    On the left on the Hierarchy search bar I typed hori...and got some results of Horizontal_Doors_Kit
    But each Horizontal_Doors_Kit have some children and it's not helping me this way.

    I need to search it one by one in the scene.
    Maybe there is a trick to also show after filtering also the children ?

    So I thought maybe to use this script that make auto complete when searching in editorwindow:
    But this search only in the AssetDataBase and not the Hierarchy.

    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. namespace AutocompleteSearchField
    8. {
    9.     public class AssetDatabaseSearchExample : EditorWindow
    10.     {
    11.         [MenuItem("Window/Autocomplete Searchbar/AssetDatabase Search Example")]
    12.         static void Init()
    13.         {
    14.             GetWindow<AssetDatabaseSearchExample>("AssetDatabase Example").Show();
    15.         }
    16.  
    17.         [SerializeField]
    18.         AutocompleteSearchField autocompleteSearchField;
    19.  
    20.         void OnEnable()
    21.         {
    22.             if (autocompleteSearchField == null) autocompleteSearchField = new AutocompleteSearchField();
    23.             autocompleteSearchField.onInputChanged = OnInputChanged;
    24.             autocompleteSearchField.onConfirm = OnConfirm;
    25.         }
    26.  
    27.         void OnGUI()
    28.         {
    29.             GUILayout.Label("Search AssetDatabase", EditorStyles.boldLabel);
    30.             autocompleteSearchField.OnGUI();
    31.         }
    32.  
    33.         void OnInputChanged(string searchString)
    34.         {
    35.             autocompleteSearchField.ClearResults();
    36.             if (!string.IsNullOrEmpty(searchString))
    37.             {
    38.                 foreach (var assetGuid in AssetDatabase.FindAssets(searchString))
    39.                 {
    40.                     var result = AssetDatabase.GUIDToAssetPath(assetGuid);
    41.                     if (result != autocompleteSearchField.searchString)
    42.                     {
    43.                         autocompleteSearchField.AddResult(result);
    44.                     }
    45.                 }
    46.             }
    47.         }
    48.  
    49.         void OnConfirm(string result)
    50.         {
    51.             var obj = AssetDatabase.LoadMainAssetAtPath(autocompleteSearchField.searchString);
    52.             Selection.activeObject = obj;
    53.             EditorGUIUtility.PingObject(obj);
    54.         }
    55.     }
    56. }
    57.  
    Maybe somehow to make it to search in the Hierarchy and then to manipulate the results to show the gameobjects found and also the children of it ?

    This is a link for the AutocompleteSearchField script:

    https://github.com/marijnz/unity-au...eld/Scripts/Editor/AutocompleteSearchField.cs
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Not sure if it's helpful but you could search like you're doing now, select all of the GameObjects with Ctrl+A or shift-clicking them and then hit the X to delete the search query and the hierarchy will return to normal but have all of your objects still selected. Not really sure what you're trying to accomplish so it's hard to help more.