How we save oodles of time in Unity when working with prefabs
by Cale Bradbury, Developer
Last time, we helped speed up a common difficult workflow with updating models and rigs in Unity. In this post, I’ll be describing the features of our Prefab Dropdown Attribute, a tool that we created to speed up selecting prefabs during prototyping in Unity.
What does it do?
In short, it turns this
This Attribute turns the variable underneath into a dropdown in the inspector. The dropdown contains a list of all .prefab files that have a component of the same type as the attributed variable.
Our drawer also features a button that will take you to the selected prefab, so you can quickly jump over and edit some of its values.
By default it only searches for prefabs in the Prefab folder, but this can be changed at the top of PrefabDropdownDrawer.cs
There is also the option to specify a subpath on a per tag basis, say you want it to only pull from a specific subfolder of prefabs
How does it work?
PropertyDrawers are my personal favourite part of Unity’s editor. We love custom editors in Unity but by no means want to write one for every script we create. By creating a PropertyAttribute tag and a PropertyDrawer to go along with it you can quickly cobble together the parts of an editor that you would want for a class without needing to waste time going through the motions of setting up files, grabbing components from past editors, etc.
The basic premise for how this script works is pretty simple:
- Scan your specified folder for any and all .prefab files,
- Try to get a component that is the same type as the attributed variable,
- Display the name of each valid prefab in the inspector using Unity’s default EditorGUI Popup function.
What’s next to improve here? We could have done some caching of valid prefabs. This might happen in the future but it works well for our use cases for now and hasn’t slowed us down. Comment below if you have suggestions, or submit a pull request if you’d like to contribute.
We hope this this helps you speed up your prototyping and development!
The Prefab Dropdown is available from our GitHib page.