Dear all,
I’m trying to customize Unity Editor, and the following problem keep bothering me.
To make it brief: Can the OnInspectorGUI() of Customized Editor behaves like a Constructor **that can call its components’ OnInspectorGUI() **? How? Likewise for the derived class, how can I call its base class’s OnInspectorGUI()?
For Component Case:
Suppose I’m writing class Player, and it has a Component: CharacterAttribute. And now I’m writing the Customized Editor for Player, can I call the Editor of CharacterAttribute?
// In "Player.cs":
public class Player : MonoBehaviour
{
CharacterAttribute attribute;
[SerializeField] Armer armer;
}
// In "CharacterAttribute.cs", it has Customized Editor already defined in "CharacterAttributeEditor.cs"
public class CharacterAttribute : MonoBehaviour
{
float healthPoins;
}
// In "PlayerEditor.cs":
[CustomEditor( typeof(Player), true )]
public class PlayerEditor: Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI (); // this seems only display "armer" in the Inspector, but no CharacterAttribute
}
}
For Inheritance Case:
For inheritance case, suppose I have a abstract class Human, it define some basic Live attribute. Then I define a custom Editor for abstract class Human.
Latter, I define a class Warrior that inherit from Human, and has extra data members such as Attack attribute (thus a Editor with extra column should be added.). However, the base.OnInspectorGUI() of Warrior seems will not call the Editor of Human at all…
I have tried to use another approach by eliminate the HumanEditor. Instead, in class Human I define a drawInspector() that contained the original content in HumanEditor.OnInspectorGUI(). And will be called by WarriorEditor.OnInspectorGUI(). This approach can fulfill what I want in the Inspector but kind of silly. Moreover, I can’t use the Unity default inspector of class Warrior by base.OnInspectorGUI(). Otherwise, not only the Warrior will be Draw, but also the Human…
// Human.cs
[System.Serializable]
public abstract class Human : MonoBehaviour // its Editor has been customized
{
[SerializeField] private float health;
public void drawInspector() //define a Editor callback
{
// Draw the Basic Inspector for Human
EditorUtility.SetDirty (this);
}
}
// Warrior.cs
public class Warrior : Human
{
float attack;
}
// WarriorEditor.cs
[CustomEditor( typeof(Warrior) )]
public class WarriorEditor : Editor
{
public override void OnInspectorGUI()
{
Warrior warrior = this.target as warrior;
warrior.drawInspector(); //display the Inspector of Human
}
}
Any idea? Thanks.
PS. I’m using Unity 4.6.1