I’m writing a GUIControl base class for UI elements so that I can edit them in real-time, and hopefully persist prototypes as assets. That’s the idea - so I figured deriving from System.Object would make sense.
Unfortunately, I’m running into some extremely weird behaviour. I know that the constructor can be called at anytime, so I made an Init function that I call explicitly after constructing an empty version.
That’s works fine until I save a script, and then it clears my list fields (children and initChildren are both null) but does not clear my string field (initState is “initialized”). I can see the constructor being called, then the references are gone, and the string field seems untouched.
class GUIControl extends Object
{
var name : System.String;
var parent : GUIControl;
var children : Object[];
var position : Vector2;
var size : Vector2;
var visible : boolean = true;
var initState : System.String = "empty";
var initChildren : Array;
function GUIControl()
{
Debug.Log( "GUIControl.GUIControl: initState='" + initState + "'" );
}
function Init()
{
Debug.Log( "GUIControl.Init" );
// intialize to empty list
children = new System.Object[ 0 ];
initChildren = new Array();
initState = "initialized";
}
function AddChild( control : GUIControl )
{
// add child to array
children = MiscUtils.BuiltinArrayAdd( children, control );
initChildren.Add( control );
// setup backpointer
control.parent = this;
}
virtual function OnGUI( screenPosition : Vector2 )
{
// check visibility
if( !visible )
return;
// what the...
if( children == null )
{
Debug.Log( "No children and initState=" + initState + ", initChildren=" + initChildren );
return;
}
}
}
So I really need some insight as to what the proper procedure here should be; how do I protect my objects against whatever comes along and strips the lists away? Implement some deep serialization? Derive from another base class entirely?
I tried a putting a list field in a MonoBehaviour, and saw the same result. Tried deriving from ScriptableObject, also same result, even though the docs indicate they’re for holding data.