Last active
November 11, 2024 03:45
-
-
Save joshcamas/cef9f029889cc697356ecb7fe7317b52 to your computer and use it in GitHub Desktop.
Unity3D get / set project window search content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* References: | |
* https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectWindow/SearchFilter.cs | |
* https://github.com/jamesjlinden/unity-decompiled/blob/master/UnityEditor/UnityEditor/ProjectBrowser.cs | |
*/ | |
public class ProjectWindowHelper | |
{ | |
public struct ProjectSearch | |
{ | |
public string filter; | |
public string[] assetLabels; | |
public string[] classNames; | |
//Debugging | |
public override string ToString() | |
{ | |
string str = ""; | |
if (filter != null) | |
str += "SearchFilter: " + filter; | |
if (assetLabels != null && assetLabels.Length > 0) | |
{ | |
str += " Labels: ["; | |
for (int i = 0; i < assetLabels.Length;i++) | |
{ | |
if (i > 0) | |
str += ","; | |
str += assetLabels[i]; | |
} | |
str += "] "; | |
} | |
if (classNames != null) | |
{ | |
str += " Class Names: ["; | |
for (int i = 0; i < classNames.Length; i++) | |
{ | |
if (i > 0) | |
str += ","; | |
str += assetLabels[i]; | |
} | |
str += "] "; | |
} | |
return str; | |
} | |
} | |
//Returns project window | |
private static EditorWindow GetProjectWindow() | |
{ | |
EditorWindow[] windows = (EditorWindow[])Resources.FindObjectsOfTypeAll(typeof(EditorWindow)); | |
EditorWindow projectWindow = null; | |
foreach (EditorWindow window in windows) | |
{ | |
if (window.GetType().ToString() == "UnityEditor.ProjectBrowser") | |
{ | |
projectWindow = window; | |
break; | |
} | |
} | |
return projectWindow; | |
} | |
//Forces project search string | |
public static void SetProjectSearch(string filter) | |
{ | |
EditorWindow projectWindow = GetProjectWindow(); | |
if (projectWindow == null) | |
return; | |
MethodInfo setSearchType = projectWindow.GetType().GetMethod("SetSearch", new[] { typeof(string) }); | |
setSearchType.Invoke(projectWindow, new object[] { filter }); | |
} | |
//Forces project search | |
public static void SetProjectSearch(ProjectSearch projectSearch) | |
{ | |
EditorWindow projectWindow = GetProjectWindow(); | |
if (projectWindow == null) | |
return; | |
System.Type searchFilterType = projectWindow.GetType().GetField("m_SearchFilter", BindingFlags.Instance | BindingFlags.NonPublic).FieldType; | |
MethodInfo setSearchType = projectWindow.GetType().GetMethod("SetSearch", new[] { searchFilterType }); | |
object searchFilterObject = System.Activator.CreateInstance(searchFilterType); | |
searchFilterType.GetField("m_NameFilter", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(searchFilterObject, projectSearch.filter); | |
searchFilterType.GetField("m_ClassNames", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(searchFilterObject, projectSearch.classNames); | |
searchFilterType.GetField("m_AssetLabels", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(searchFilterObject, projectSearch.assetLabels); | |
setSearchType.Invoke(projectWindow, new object[] { searchFilterObject }); | |
} | |
//Returns project search string (tags / labels not included!!!) | |
public static string GetProjectSearchString() | |
{ | |
EditorWindow projectWindow = GetProjectWindow(); | |
if (projectWindow == null) | |
return null; | |
FieldInfo field = projectWindow.GetType().GetField("m_SearchFilter", BindingFlags.Instance | BindingFlags.NonPublic); | |
object filter = field.GetValue(projectWindow); | |
return (string)field.FieldType.GetField("m_NameFilter", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(filter); | |
} | |
//Returns project search string (tags / labels not included!!!) | |
public static ProjectSearch GetProjectSearch() | |
{ | |
EditorWindow projectWindow = GetProjectWindow(); | |
if (projectWindow == null) | |
return new ProjectSearch(); | |
FieldInfo field = projectWindow.GetType().GetField("m_SearchFilter", BindingFlags.Instance | BindingFlags.NonPublic); | |
object searchFilter = field.GetValue(projectWindow); | |
ProjectSearch projectSearch = new ProjectSearch(); | |
projectSearch.filter = (string)field.FieldType.GetField("m_NameFilter", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(searchFilter); | |
projectSearch.classNames = (string[])field.FieldType.GetField("m_ClassNames", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(searchFilter); | |
projectSearch.assetLabels = (string[])field.FieldType.GetField("m_AssetLabels", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(searchFilter); | |
return projectSearch; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment