Last active
April 7, 2021 11:20
-
-
Save roguesleipnir/17e67896ffe2c94e781b2790121a7f79 to your computer and use it in GitHub Desktop.
Quick search for components and serialized properties in prefabs.
This file contains hidden or 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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
public class SearchForProperties : EditorWindow { | |
MonoScript targetComponent; | |
string propertyString; | |
bool hideVariants; | |
List<string> searchResults = new List<string> (); | |
Vector2 scroll; | |
public string[] GetAllPrefabs () { | |
string[] temp = AssetDatabase.GetAllAssetPaths (); | |
List<string> result = new List<string> (); | |
foreach (string s in temp) { | |
if (s.Contains (".prefab")) result.Add (s); | |
} | |
return result.ToArray (); | |
} | |
public bool AdvancedFilter (GameObject obj) { | |
return true; | |
} | |
[MenuItem ("Tools/Search For Properties")] | |
public static void Open () { | |
GetWindow<SearchForProperties> (); | |
} | |
void OnGUI () { | |
targetComponent = (MonoScript)EditorGUILayout.ObjectField(targetComponent, typeof(MonoScript), false); | |
propertyString = EditorGUILayout.TextField ("Property Search", propertyString); | |
hideVariants = GUILayout.Toggle(hideVariants, "Hide Variant Prefabs"); | |
if (GUILayout.Button ("Search")) { | |
searchResults.Clear (); | |
string targetPath = AssetDatabase.GetAssetPath(targetComponent); | |
string[] allPrefabs = GetAllPrefabs(); | |
foreach (string prefab in allPrefabs) { | |
GameObject obj = (GameObject)AssetDatabase.LoadAssetAtPath (prefab, typeof (GameObject));; | |
string[] single = new string[] { prefab }; | |
string[] dependencies = AssetDatabase.GetDependencies (single, false); | |
var addResult = false; | |
if (targetComponent == null) { | |
addResult = true; | |
} | |
else { | |
foreach (string dependedAsset in dependencies) { | |
if (dependedAsset == targetPath) { | |
addResult = true; | |
} | |
} | |
} | |
if (hideVariants && addResult) { | |
var isVariant = PrefabUtility.GetPrefabAssetType (obj) == PrefabAssetType.Variant; | |
addResult = addResult && !isVariant; | |
} | |
if (addResult && propertyString != null && propertyString.Length > 0) { | |
addResult = addResult && SearchSerializedProperty (propertyString, prefab); | |
} | |
if (addResult) | |
addResult = addResult && AdvancedFilter (obj); | |
if (addResult) | |
searchResults.Add (prefab); | |
} | |
} | |
searchResults.Sort (SortAlphabetically); | |
scroll = GUILayout.BeginScrollView (scroll); | |
{ | |
for (int i = 0; i < searchResults.Count; i++) { | |
GUILayout.BeginHorizontal (); | |
{ | |
GUILayout.Label (Path.GetFileNameWithoutExtension (searchResults[i])); | |
GUILayout.FlexibleSpace (); | |
if (GUILayout.Button ("Show")) | |
EditorGUIUtility.PingObject (AssetDatabase.LoadAssetAtPath (searchResults[i], typeof (GameObject))); | |
} | |
GUILayout.EndHorizontal (); | |
} | |
} | |
GUILayout.EndScrollView (); | |
} | |
int SortAlphabetically(string a, string b) { | |
return a.CompareTo(b); | |
} | |
public bool SearchSerializedProperty (string match, string path) { | |
string projectPath = Application.dataPath; | |
projectPath = projectPath.Substring(0, projectPath.IndexOf("Assets")); | |
try { | |
System.IO.FileStream FS = new System.IO.FileStream (projectPath + path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); | |
System.IO.StreamReader SR = new System.IO.StreamReader (FS); | |
string line; | |
int lineNum = 0; | |
while (!SR.EndOfStream) { | |
lineNum++; | |
line = SR.ReadLine (); | |
int index = line.IndexOf (match); | |
if (index != -1) { | |
return true; | |
} | |
} | |
} | |
catch { | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment