- Home /
How to access elements in an array through a SerializedProperty?
I am trying to modify the elements in an array via a SerializedProperty. However, I cannot seem to figure out how to. I can increase the array size and such, but I can't seem to directly access and modify an element in the array.
When it comes to single values, say a float, I can modify it as such:
SerializedProperty m_Speed;
public void OnEnable()
{
m_Speed = this.serializedObject.FindProperty("speed");
}
public override void OnInspectorGUI()
{
this.serializedObject.Update();
EditorGUILayout.PropertyField(m_Speed);
if(m_Speed.floatValue < 0)
{
//Prevents value from going below 0
m_Speed.floatValue = 0f;
}
this.serializedObject.ApplyModifiedProperties();
}
How can I do this with an array?
Answer by YoucefB · Oct 11, 2017 at 04:53 PM
Here is how it's done:
SerializedProperty m_Speed;
public void OnEnable()
{
m_Speed = this.serializedObject.FindProperty("speed");
}
public override void OnInspectorGUI(){
serializedObject.Update ();
for (int x = 0; x < m_Speed.arraySize; x++) {
SerializedProperty property = m_Speed.GetArrayElementAtIndex (x); // get array element at x
property.floatValue = Mathf.Max (0,property.floatValue); // Edit this element's value, in this case limit the float's value to a positive value.
}
EditorGUILayout.PropertyField(m_Speed,true); // draw property with it's children
serializedObject.ApplyModifiedProperties ();
}
This is excellent, one last question before I mark this as the answer. How might this be done if the array is for GameObjects. Using your example above, would it be property.objectReferenceInstanceIDValue = new GameObject("obj")
or property.objectReferenceValue = new GameObject("obj")
?
Yes the second one, so you want to fill the array with gameObjects, here is a quick example:
SerializedProperty m_objects;
List<GameObject> tempObjects;
public void OnEnable()
{
m_objects = this.serializedObject.FindProperty("objects");
tempObjects = new List<GameObject> ();
for (int x = 0; x < m_objects.arraySize; x++) {
tempObjects.Add((GameObject)m_objects.GetArrayElementAtIndex (x).objectReferenceValue);
}
}
public override void OnInspectorGUI(){
serializedObject.Update ();
for (int x = 0; x < m_objects.arraySize; x++) {
SerializedProperty property = m_objects.GetArrayElementAtIndex (x) ; // get array element at x
if (property.objectReferenceValue == null) {
GameObject newObject = new GameObject ("object " + x);
property.objectReferenceValue = newObject;
tempObjects.Add(newObject); // store it in the temp List
}
}
EditorGUI.BeginChangeCheck (); // check if the size of the array has changed or one of the elements has been changed.
EditorGUILayout.PropertyField(m_objects,true); // draw property with it's childs
if (EditorGUI.EndChangeCheck()){
for (int x = 0; x < tempObjects.Count; x++) {
if (x >= m_objects.arraySize || (tempObjects!=null&&m_objects.GetArrayElementAtIndex (x).objectReferenceValue==null))
DestroyImmediate (tempObjects[x].gameObject); // destroy the Objects that are not in the array
}
}
serializedObject.Apply$$anonymous$$odifiedProperties ();
}
Your answer
Follow this Question
Related Questions
Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer
Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers
How do I delete or empty an array of custom structs during edit mode? 1 Answer
Easy way to set multiple SerializedProperties 1 Answer
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers