Search Unity

Resolved Search for material name

Discussion in 'Authoring Dev Blitz Day 2023' started by DevDunk, Jan 25, 2023.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,057
    I personally would like to search for material names and see all the renderers with that material in the scene view. Especially when working with atlassing this can be very useful.
    Preferably in the hiarchy window, but the Search tab also would be fine

    Would love to know if this would be possible!
     
    PutridEx and adamgolden like this.
  2. justinas_p

    justinas_p

    Unity Technologies

    Joined:
    Nov 7, 2016
    Posts:
    6
    Hi! I'm not really sure if there is a possibility to do that at this particular moment, but here is a quick sample editor script for filtering materials:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class MaterialFilter : EditorWindow
    7. {
    8.     private string materialName;
    9.  
    10.     [MenuItem("Window/Material Filter")]
    11.     public static void ShowWindow()
    12.     {
    13.         GetWindow<MaterialFilter>("Material Filter");
    14.     }
    15.  
    16.     private void OnGUI()
    17.     {
    18.         materialName = EditorGUILayout.TextField("Material Name:", materialName);
    19.  
    20.         if (GUILayout.Button("Filter"))
    21.         {
    22.             FilterByMaterialName(materialName);
    23.         }
    24.        
    25.         if (GUILayout.Button("Clear"))
    26.         {
    27.             FilterByMaterialName(string.Empty);
    28.         }
    29.     }
    30.  
    31.     private void FilterByMaterialName(string name)
    32.     {
    33.         List<GameObject> allObjects = new List<GameObject>();
    34.        
    35.         foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
    36.         {
    37.             if (!EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
    38.                 allObjects.Add(go);
    39.         }
    40.            
    41.         foreach (GameObject go in allObjects)
    42.         {
    43.             if (name == string.Empty)
    44.             {
    45.                 go.SetActive(true);
    46.                 continue;
    47.             }
    48.  
    49.             Renderer renderer = go.GetComponent<Renderer>();
    50.             if (renderer != null)
    51.             {
    52.                 Material[] materials = renderer.sharedMaterials;
    53.                 bool found = false;
    54.                 foreach (Material mat in materials)
    55.                 {
    56.                     if (mat.name == name)
    57.                     {
    58.                         found = true;
    59.                         break;
    60.                     }
    61.                 }
    62.                 go.SetActive(found);
    63.             }
    64.             else
    65.             {
    66.                 go.SetActive(false);
    67.             }
    68.         }
    69.     }
    70. }
     
    DevDunk likes this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,057
    Thanks!
     
  4. sebastienp_unity

    sebastienp_unity

    Unity Technologies

    Joined:
    Feb 16, 2018
    Posts:
    201
    Hi @DevDunk ,

    The SearchWindow supports this workflow out of the box with the CustomSceneFilter material:

    upload_2023-1-26_11-10-36.png

    upload_2023-1-26_11-10-7.png



     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Using Quick Search, you can search for things in the scene with h:, and use ref:path to get things referencing things at that path.

    Example:

    upload_2023-1-26_17-10-37.png

    You can also filter for type:

    upload_2023-1-26_17-10-53.png
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Sniped by the QS developer! Though I believe my text-based approach is supported in older versions than the neat visual query builder.
     
  7. sebastienp_unity

    sebastienp_unity

    Unity Technologies

    Joined:
    Feb 16, 2018
    Posts:
    201
    What Baste said is gold (as always).

    Visual Query Builder was added for 21.3 I think. My screenshot are from 22.2 beta.

    the keyword that Baste as used: "ref" wil find any type of reference to the specified object.

    The query I wrote use the custom "material" filter to test against the material property of MeshFilterer. It can be expressed purely as text as well:

    upload_2023-1-26_11-44-29.png

     
    Baste likes this.