- Home /
Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer)
Does anyone knows if it is possible to dynamically display GUI entries in the Editor's Inspector based on the GameObject/prefab of my choice?
Let's say I have a several instances of a player prefabs (for example various cars) and those cars have different elements such as guns, missiles launchers, defense shields etc Beside that I have a GameObject that describes all characteristics of a car (it's pricing, upgrades etc). This object has an array of my prefab cars and within that GameObject Inspector window I would like to show dynamically different GUI elements based on the prefab from the array.
Answer by KEric · Nov 06, 2015 at 01:11 PM
After doing some research (mainly watching tutorials that I'm attaching below and playing with intellisense completion in Visual Studio as well as debugging and logging to console output) I found what I was looking for.
First to have my custom editor window (or I suppose my "custom property") I needed to have respective [CustomPropertyDrawer(typeof(MyClass))]
implemented within the /Editor folder. Then within void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
method I was able to get to my MyClass::car_prefab using GameObject prefab = (GameObject)prop.FindPropertyRelative("car_prefab").objectReferenceValue;
So the shortest answer to my problem seems to be the usage of objectReferenceValue
of SerializedProperty
returned from FindPropertyRelative
function execution. Hope it helps somebody in future. Cheers
[2] https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
PS. One thing worth mentioning - because property size in Editor Inspector will differ based on the information contained within the prefab of my choice, there is a need of overriding public override float GetPropertyHeight (SerializedProperty prop, GUIContent label)
within CustomPropertyDrawer
So, in the end, when you are displaying some properties of choosen gameobject (prefab) and then changing their values in inspector, what is happening? It changes the values of properties of prefab or its instance, existing only in this particular script?
Answer by HenryStrattonFW · Nov 05, 2015 at 05:57 PM
This is very possible, but is probably easier not to do as a property drawer, but as a custom inspector (CustomEditor class).
If you're writing your own CustomEditor (inspector) for your class you just need a way to select an item from the array (easiest solution may just be to have a variable on your inspector for the current index in the array, and some buttons (next, prev, first, last, whatever you need) those buttons then just change that index value (wrapping or clamping as necessary). You can get access to the array via the "target" of the CustomEditor you can either get the object as a serializedObject or as a regular game object, whichever works for you.
You can then just check what item in the array is identified with your index, and then draw whatever other elements you want on your Inspector based on that.
If you don't want to completely remake all of the other GUI elements you may need for the inspector, you can also just code these changes, and draw the rest of the inspector using the DrawDefaultInspector method. reducing the need for you to remake all the other displays.
Hope this helps point you in the right direction.
Thanks for the answer. I don't know if I made the problem clear in my previous comment. I just want to be able to show different set of values, components (class objects) in the inspector, based on the prefab of my choice (in this case the values of the prefab would dictate the inspector's view, for example if some value, let's say array of strings in the prefab is null then nothing should be showed in the inspector based on this prefab).
Ah i see, so you have multiple prefabs and you're selecting them and want the inspector to change based on the selected object, not an array or prefabs with a selection of one of those. Ok.
Well its more or less the same thing. You just write a CustomEditor for your script. which then serves as the Inspector. in the inspector you just draw whatever values you DO want to display based on whatever key variable values you want to check on the selected object.
Have you written a custom inspector before? If not heres a usefull link to get you started. https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
I have written Custom Property years ago and also have seen building-custom-instpector tutorial. Thanks for your help, I think that I finally found what I was looking for (see the co$$anonymous$$g answer if you're interested)