- Home /
Serializedproperty assignment
question is at the end of the post
I have the following structure in my code.
[Serializable]
public class SomeClass : ScriptableObject{
public OtherClass[] others;
}
[Serializable]
public class OtherClass{
public string name;
// ....
public static OtherClass GenerateOtherClass(){ /* .... */ }
}
and there is a Custom Editor for SomeClass
[CustomEditor(typeof(SomeClass))]
public class SomeClassEditor : UnityEditor.Editor {
public override void OnInspectorGUI() {
var others = serializedObject.FindProperty("others");
// ....
if (GUILayout.Button("Generate")) {
var o = OtherClass.GenerateOther();
/** append o to 'others' **/
}
// ....
sobj.ApplyModifiedProperties();
}
}
I want to add a new OtherClass
object to the list of others
. note: OtherClass
is not a ScriptableObject
edit to clarify, the question is how do i get every variable/field/member of a Serializable-Object into a Serializeableproperty (structure) without listing every element manually end-edit would be cool if someone knows a solution for this.
Answer by troien · Mar 18, 2016 at 06:22 PM
You could use SerializedProperty.InsertArrayElementAtIndex. Note that this will insert an empty object to the array at given index.
if (GUILayout.Button("Generate"))
{
int newIndex = others.arraySize;
others.InsertArrayElementAtIndex(newIndex);
/* you can get the SerializedProperty of your new object using
"others.GetArrayElementAtIndex(newIndex);" if you need to */
}
this is the easy part (to increase the array size). But how do i get every field of a Serializable object into a SerializedProperty structure, without listing every element manually
Ah ok. Well, using the SerializedProperty there isn't a way to do that I believe (other then perhaps the more hacky like approaches of which one you used yourself)
You could also cast the Editor.target to your 'SomeClass' and add it manually through code. You do need to call Undo.RecordObject(target) before changing. (Has been a while sinse I made Editor scripts and Unity changed some things after 5.3, but I believe this still works). Note however that if you want $$anonymous$$ulti-Object editing with this, you'll need to implement that yourself...
Answer by Soraphis · Mar 18, 2016 at 11:21 PM
Well i think i got it myself. Maybe theres some nicer solution. but here is mine:
private void fillPropertyFromObject(SerializedProperty property, object o) {
if(! o.GetType().IsSerializable) throw new ArgumentException("object is not Serializable");
var it = property.Copy();
it.NextVisible(true);
var d = it.depth;
do {
var field = o.GetType().GetField(it.name);
switch (it.propertyType) {
case SerializedPropertyType.Generic:
break;
case SerializedPropertyType.Integer:
it.intValue = (int) field.GetValue(o);
break;
case SerializedPropertyType.Boolean:
it.boolValue = (bool)field.GetValue(o);
break;
case SerializedPropertyType.Float:
it.floatValue = (float)field.GetValue(o);
break;
case SerializedPropertyType.String:
it.stringValue = (string)field.GetValue(o);
break;
default:
throw new NotImplementedException();
}
} while(it.NextVisible(false) && it.depth >= d);
}
Your answer
Follow this Question
Related Questions
Finding property with serializedObject on script with a generic 0 Answers
Calling a method on a serialized property (Custom Editor with Reorderable List) 0 Answers
Incrementing a SerializedProperty on a per-object basis 1 Answer
How to update serializedField showed on the inspector by script? 1 Answer