Hello,
I have a SerializedProperty
that is referencing a List
, where Decision
is just a regular class (Not a MonoBehaviour
, nor a ScriptableObject
) mocked with [System.Serializable]
The problem I’m facing, I can’t add new decisions!
Well, the way one would add something to a list that’s referenced via a SerializedProperty
is to manually increase the size of the array (via arraySize
), and then set the last element directly, a method to do that would be (for strings):
public static void Add(this SerializedProperty prop, string value)
{
prop.arraySize++;
prop.GetAt(prop.arraySize - 1).stringValue = value;
}
The problem is that I can’t do this to add my Decision
object:
prop.arraySize++;
prop.GetAt(prop.arraySize - 1).objectReferenceValue = value;
Because, objectReferenceValue
is a UnityEngine.Object
, while Decision
is a System.Object
There’s no way to convert from the first to the second.
There are obvious ways around this, one to make the Decision
class a ScriptableObject
that way it is a UnityEngine.Object
, other is to just get a direct reference to the list and just add the element - but then I would have to register undo manually.
I would really love to just keep my class a normal class, and be able to add it to the list via the SerializedProperty
Any ideas?
Thanks!