- Home /
[JS] [Editor] SerializedObject from a custom class.
This one is a bit complicated. I've tried to find a solution for several hours, but with no luck. This is my scenario:
I have a ScriptableObject called Sprite2DHolder. It has a Generic List. called SpriteHolder of type Sprite. Sprite is a custom class that has a string, some ints, booleans, Vector2s and a List..
Then I have a monobehaviour Sprite2DPlayer, that holds a reference to the ScriptableObject. Sprite2DPlayer also has a custom editor. Now, this is what I'm trying to achieve:
I want Undo/Redo support (if possible even MultiO editting), so I'm using SerializedObjects and Properties. Everything worked fine up until the moment I wanted to Serialize the SpriteHolder variable. It's a List, but I only need to access 1 element of it at a time, so I've created a SerializedObject from it, but it's not working.
Simplified version of my code:
@CustomEditor(Sprite2DPlayer)
class Sprite2DPlayerEditor extends Editor {
private var script : SerializedObject;
private var realScript : Sprite2DPlayer;
private var HolderVar : SerializedProperty;
private var Holder : SerializedObject;
private var FrameDuration : SerializedProperty;
function OnEnable () {
script = new SerializedObject(target);
realScript = target as Sprite2DPlayer;
HolderVar = script.FindProperty("Holder");
if(HolderVar.objectReferenceValue != null) {
Holder = SerializedObject(HolderVar.objectReferenceValue);
var holderVarProp : SerializedProperty = Holder.FindProperty("SpriteHolder"); // this works
var singleElementOfHolderVar = Holder.FindProperty("SpriteHolder.Array.data[0]").objectReferenceValue; // this doesn't
}
}
First it finds the property "Holder" that's in Sprite2DPlayer.js, if it's not null, it wraps it into a SerializedObject. Under that code I have 2 SerializedProperties, the first one is the whole array, this works, kinda. When I try to access an element with holderVarProp.GetArrayElementAtIndex(0), it returns a SerializedProperty correctly, but I can't modify any of the variables, because when I try to cast it to a Sprite, I get Null or 'type is not a supported pptr value' when I try to first get .objectReferenceValue and than cast it.
The second method (singleElementOfHolderVar) doesn't work at all.
--- David
Your answer
Follow this Question
Related Questions
What would be the best way to go about loading and saving a Serializable object in JSON with Unity? 0 Answers
Is there a way to get the names of all SerializedProperty objects in a SerializedProperty object? 1 Answer
Display function parameters in inspector 0 Answers
Dynamic serialized fields based on enum 0 Answers