Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to clear the search bar via C#

Discussion in 'Scripting' started by petarnordeus, Aug 13, 2019.

  1. petarnordeus

    petarnordeus

    Joined:
    Aug 4, 2017
    Posts:
    7
    Capture.PNG

    Is there a way to clear the project browser search bar?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You'll have to do it using reflection.

    The Project browsers class is internal, and the clear search method is private. The source is here.

    You can find the project view instances by calling Resources.FindObjectOfTypeAll. That's how Unity finds editor windows internally in quite a few cases. Again, since the ProjectBrowser Since the type's internal, you'll either have to use EditorWindow as the search parameter and filter for type name, or call that method through reflection as well.
     
    petarnordeus likes this.
  3. petarnordeus

    petarnordeus

    Joined:
    Aug 4, 2017
    Posts:
    7
    OMG! I did it :)

    here's the code if anyone needs it:

    Code (CSharp):
    1. var pb = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
    2.         var ins = pb.GetField("s_LastInteractedProjectBrowser", BindingFlags.Static| BindingFlags.Public).GetValue(null);
    3.         var method = pb.GetMethod("ClearSearch", BindingFlags.NonPublic|BindingFlags.Instance);
    4.         method.Invoke(ins, null);
    Thanks Baste
     
    Lukebox and Baste like this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    I didn't spot LastInteractedProjectBrowser, nice catch.