- Home /
Editor extension, How do I Serialized nested class?
What happen is that My script use lots of nested Class. I was trying to use SerializedObject but without success. How do I show nested class properties on the Inspector?
SerializedObject.Update
//Plenty of InspectorGUI
SerializedObject.ApplyModifiedProperties
Wanted to do something like above.
///////////////////////////////////////////////////////////////////////
[Below] Current Code
using UnityEngine;
using UnityEditor;
using System.Collections;
[CanEditMultipleObjects]
[CustomEditor(typeof(Unit))]
public class UnitEditor : Editor {
Unit thisUnit;
string unitName;
UNITCLASS currentClass;
UnitIntVal HP;
UnitIntVal AP;
void OnEnable ()
{
thisUnit = (Unit)target;
}
public override void OnInspectorGUI()
{
UpdateProperties();
EditorGUI.BeginChangeCheck ();
PropertiesDrawer();
if (EditorGUI.EndChangeCheck () /*&& thisUnit.thisClass.currentClass != currentClass*/)
{
DoIfPropertyChanged();
}
}
////////////////////////////////////////////////////////////
void UpdateProperties()
{
unitName = thisUnit.GetName();
currentClass = thisUnit.GetClass();
HP = thisUnit.GetHP();
AP = thisUnit.GetAP();
}
/////////////////////////////////////////
void PropertiesDrawer()
{
DrawNameProp();
DrawClassProp();
DrawHPProp();
DrawAPProp();
DrawDefaultInspector();
}
//////////
void DrawNameProp()
{
unitName = EditorGUILayout.TextField("Name", unitName);
}
void DrawClassProp()
{
currentClass = (UNITCLASS)EditorGUILayout.EnumPopup("Class", currentClass );
}
void DrawHPProp()
{
HP.valMax = EditorGUILayout.IntField("MaxHP", HP.GetValMax());
HP.val = EditorGUILayout.IntSlider( "HP "+HP.GetVal()+"/" +HP.GetValMax(), HP.GetVal(),0,HP.GetValMax());
GUILayout.Space(5.0f);
}
void DrawAPProp()
{
AP.valMax = EditorGUILayout.IntField("MaxAP", AP.GetValMax());
AP.val = EditorGUILayout.IntSlider( "AP "+AP.GetVal()+"/"+AP.GetValMax(), AP.GetVal(),0,AP.GetValMax());
GUILayout.Space(5.0f);
}
/////////////////////////////////////////
void DoIfPropertyChanged()
{
thisUnit.SetName(unitName);
thisUnit.SetClass(currentClass);
thisUnit.SetHP(HP);
if(HP.valMax < 1) HP.SetValMax(1);
thisUnit.SetAP(AP);
if(AP.valMax < 1) AP.SetValMax(1);
}
}
////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
[System.Serializable]
public enum UNITCLASS{ TROOPER, SENTINEL, STRIKER, LAST };
[System.Serializable]
public class UnitClass {
public UNITCLASS currentClass;
public UnitClass()
{
currentClass = UNITCLASS.TROOPER;
}
public UnitClass( UNITCLASS newClass )
{
currentClass = newClass;
}
//////////////////////////////////////////////
public UNITCLASS GetClass(){ return currentClass; }
public void SetClass( UNITCLASS newClass ) { currentClass = newClass; }
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613101748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Serializable class variables loose Gameobject reference in inspector 0 Answers
Hiding editor extension scripts 2 Answers
layout serializable objects in unity 1 Answer
How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer
Instantiating a prefab in the editor is orphaning the children of the prefabs 0 Answers