Hi all,
I’ve just started to look at how (if possible!) I can alter the inspector dynamically.
For example:
I might have:
bool attackable = true;
attackable_modes attack_mode; //a list of enums
Now I can adjust all this in the editor which is great. But what I really want is if I change attackable to false then the attackable_modes enum choice isn’t available (I hope that’s clear).
Is there an easy way to have the inspector be ‘dynamic’?
Thanks
If you use a Custom Inspector for that object it’s pretty quick/easy.
//This is your class you want to use
public class MyExampleClass : MonoBehaviour
{
public enum AttackStates
{
First,
Second,
Third
}
public AttackStates attackState;
public bool isAttackable;
}
//This goes into the Editor folder as a Custom Inspector
[CustomEditor(typeof(MyExampleClass))]
public class MyExampleEditor : Editor
{
public override void OnInspectorGUI()
{
MyExampleClass target = (MyExampleClass)this.target;
if(target.isAttackable = EditorGUILayout.Toggle("Attackable:", target.isAttackable))
{
target.attackState = (MyExampleClass.AttackStates)EditorGUILayout.EnumPopup("AttackState", target.attackState);
}
}
}
But it also means you need to manage the Inspector visuals entirely yourself, unless you start playing around and purposely hide those values and use Editor.DrawDefaultInspector().
Hey Ntero,
Thanks!;Hmmmmm, I could end up with a load of custom editor scripts then 
Shame it can’t be done ‘runtime’ (as it were) and actually within the script.
Thanks anyway!
Cheers
One trick you can use to reduce the amount of bloat making all sorts of Custom Inspectors is to make a list of Static Functions that display particular Interfaces.
That way you can just call InspectorHelper.RenderAttackable(target); for each type of object that can attack. And use other interfaces for shared behaviours in other objects. Doesn’t reduce the amount of Custom Inspectors, but reduces the amount of code in each, and puts it all in a single, more manageable helper class.