- Home /
Question by
Charcolle · Dec 20, 2017 at 02:00 PM ·
c#editoreditor-scriptingeditorwindowconstructor
Undo.RecordObject and Constructor of Serializable Class
I cannot understand why constructor of serializable class is called when doing Undo.RecordObject.
Here is my sample editor-window code.
public class ConstructorTest: EditorWindow {
[MenuItem( "Window/Test/Constructor" )]
static void Open() {
var win = GetWindow<ConstructorTest>();
win.Show();
Undo.undoRedoPerformed += win.Repaint;
}
[SerializeField]
public List<Test1> TestList = new List<Test1>();
private Vector2 ScrollView;
private void OnGUI() {
EditorGUI.BeginChangeCheck();
GUILayout.Label( "Constructor Test" );
EditorGUILayout.BeginVertical();
{
ScrollView = EditorGUILayout.BeginScrollView( ScrollView );
{
for ( int i = 0; i < TestList.Count; i++ )
TestList[i].OnGUI( i + 1 );
}
EditorGUILayout.EndScrollView();
}
EditorGUILayout.EndVertical();
GUILayout.FlexibleSpace();
if( GUILayout.Button( "Add" ) ) {
Undo.RecordObject( this, "add obj" );
TestList.Add( new Test1() );
}
if( EditorGUI.EndChangeCheck() )
EditorUtility.SetDirty( this );
}
}
[Serializable]
public class Test1 {
public Test2 test2 = null;
public Test1() {
Debug.Log( "Test1 Constructor" );
}
public void OnGUI( int idx ) {
EditorGUILayout.BeginVertical( GUI.skin.box );
{
GUILayout.Label( idx.ToString() );
if( test2 != null ) {
GUILayout.Label( "test2 is not null" );
}
}
EditorGUILayout.EndVertical();
}
}
[Serializable]
public class Test2 {
public Test2() {
Debug.Log( "Test2 Constructor" );
}
}
I never write new Test2(), however constructor of Test2 class was called.
When I comment out Undo.RecordObject, constructor of Test2 class was not called.
Why Undo.RecordObject call constructor of Test2 class?
I want to save Test2 Class as null at Test1 Class..
(2017.3.0f2)
editorgui-constructor.png
(11.0 kB)
Comment
Your answer
Follow this Question
Related Questions
OnGui elements as objects? 2 Answers
Close script tab in VS, from script 0 Answers
Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer