- Home /
hierarchy "ref:" search, but in script
I want this functionality: in script form. Does This exist?
You can search for all scene references of an asset with that handy find window: ex. "ref:sounds/mysound.wav" will find all references to that sound in the scene and set the seleciton.
I want this same functionality (setting the selection) except I want to be able to do it from script: ex. FindReferences("ref:sounds/mysound.wav"); //sets Selection to all scene references of the passed in asset path.
Does this exist? If not, how can I get this (specifically for sound references preferably)?
Thanks!
Answer by Jamora · Sep 18, 2013 at 12:54 PM
It is possible, but you will probably need to do it yourself.
Things you'll need:
EditorWindow/Custom Editor/Static Editor Script
Things you might need:
You'll need a search field first. You can hook into the hierarchy search bar with EditorApplication.searchChanged. I didn't find any way to modify the result set, So it might be best to make your own search in an EditorWindow and display the results there.
I think the best way to loop through only the script files is to maintain a list of all script-assets in the project somewhere by creating an AssetPostProcessor that keeps track of all changes to the assets. This is the only way I found to efficiently find all script files in the project. Then you just need to do some parsing to get the script files that contain a declaration for an AudioClip, and simply FindObjectsOfType(typeof(FoundType))
multiple times. If this is not exactly what you need, you might have to use reflection.
When going the reflection route, first I would check if the search string contains the prefix for any special implementation you want (ref:). then if the given string actually refers to an existing asset. If it does, then we'll need all MonoBehaviours in the scene. Those can be found with FindObjectsOfType(typeof(MonoBehaviour))
. Finally, you only (...) need to use reflection on each of those objects to find out any declared AudioClip field and check if it references the given asset.
Note: for some reason I always got two callbacks for the OnSearchChanged. You'll have to come up a way to dismiss the other if this also happens to you.
Answer by gjf · Sep 18, 2013 at 02:39 PM
you'd probably be doing it with this - doubt you'd need to resort to reflection...
Hmm, yes, possibly. $$anonymous$$aybe I understood the question wrong.
I understood the question to be such, that he wanted all the scripts that referenced whatever asset he put in the search box. For this he'll need to read all variables in all scripts and check what they reference.