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.
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
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
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.