- Home /
[solved] How to edit an array of custom classes in the Inspector
This is for C# !
How to edit an array of custom classes in the Inspector which was inherited from a base class?
I have a class BuildCondition : UnityEngine.Object which holds two enums (ability and potency) eg. ("buildUnits","heavy")
To check whether an unit is build able by another I want to compare the set of BuildConditions the builder meets with the set of BuildConditions the subject requires.
So in the base class Unit : MonoBehaviour there is an array of required BuildConditions.
UnitTypes eg. class UnitHarvester inherit Unit and add their own attributes eg. harvestingSpeed
So I would like to edit all Attributes trough the inspector so I can set those values for each unitType prefab easily. I have tried adding a Editor for Unit but the unitTypes stick with the array Interface which does not allow to edit the field values. So I wrote Editors for the unitTypes as well and inherit those from the base class's Editor. Like: public override void OnInspectorGUI() { base.OnInspectorGUI(); //doing the all the child Attributes } That UI works.. but it changes the values of every UnitType at once. So there is no individual memory for different objects. And the storage behaviour is .. weird.
Or am I missing the simple method of just telling the inspector to go for the Standard-Array-UI then displaying each field as the two enums it is.
Code examples: (public Attributes just for simplicity)
//BuildCondition
[System.Serializable]
public class BuildCondition : UnityEngine.Object {
public enum Abilities { buildUnits, techSupport, scienceSupport,.. }
public Abilities ability { get; set; }
public enum Potencies { light, middle, heavy, super }
public Potencies potency { get; set; }
}
//unit base class
public class Unit : MonoBehaviour {
public BuildCondition[] buildConditions;
public bool isBuildable (BuildCondition[] meetConditions) {..}
..
}
//eg. script class to be added as component
public class UnitHarvester : Unit {
public float harvestingSpeed;
..
}
Answer by whydoidoit · Jan 13, 2013 at 08:04 PM
Unless you write a custom editor then you need to make the attributes of your class fields and not properties.
Answer by EvanTreborn · Jan 13, 2013 at 09:54 PM
After two days of frustration and suffering it suddenly works.. without custom editor.. just after watching a film and having some distance.. maybe the reload of unity did it but it works now!.. I have no clue why it did not before though.
Class BuildCondition should not inherit from Object. Remove that and it'll work.