- Home /
Custom Inspector Array Elements Not Updating
I have looked everywhere for this answer, and no one seems to have this particular problem. I wrote a custom inspector for a ScriptableObject that allows me to add, and remove elements of an array. These elements are a custom class that do not extend anything - they're just a container for data (right now, functionality will be added later). The problem is that when I edit fields within an element, it does not update. Immediately upon hitting "Enter" the field will revert back to the default value. I've seen people complain about this happen upon hitting play, but never instantly after attempting to enter new values.
The inspector:
using UnityEditor;
using UnityEngine;
using System;
[CanEditMultipleObjects]
[CustomEditor(typeof(MoveStateList))]
public class MoveStateEditor : Editor
{
// A reference to our target movestate list
private MoveStateList MoveStates;
private bool showState = false;
private static GUIContent
insertContent = new GUIContent("+", "Add a MoveState"),
deleteContent = new GUIContent("-", "Delete this MoveState");
private static GUILayoutOption
buttonWidth = GUILayout.MaxWidth(20f);
void Awake()
{
MoveStates = (MoveStateList)target;
}
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
int i = 0;
GUILayout.Label("Move States", EditorStyles.boldLabel);
EditorGUI.indentLevel++;
foreach(BaseMoveState state in MoveStates.MoveStates)
{
EditorGUI.indentLevel++;
//i++;
// Add the foldout for elements, and the add/remove buttons for each state
GUILayout.BeginHorizontal();
showState = EditorGUILayout.Foldout(showState, state.Name);
if(GUILayout.Button(insertContent, EditorStyles.miniButtonLeft, buttonWidth))
{
MoveStates.AddMoveState(i);
break;
}
GUILayout.BeginVertical();
if(GUILayout.Button(deleteContent, EditorStyles.miniButtonRight, buttonWidth))
{
MoveStates.RemoveMoveState(state);
break;
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
// If the element is expanded, show the properties
// Side note - if anyone knows how to get only INDIVIDUAL elements to
// expand I'd love to hear about it
if(showState)
{
EditorGUILayout.TextField("Name", state.Name);
EditorGUILayout.FloatField("Acceleration", state.Acceleration);
EditorGUILayout.FloatField("Decceleration", state.Decceleration);
EditorGUILayout.FloatField("Turn Speed", state.TurnSpeed);
EditorGUILayout.FloatField("Bank Speed", state.BankSpeed);
EditorGUILayout.FloatField("Max Bank Angle", state.MaxBankAngle);
EditorGUILayout.FloatField("Climb Speed", state.ClimbSpeed);
EditorGUILayout.FloatField("Max Climb Angle", state.MaxClimbAngle);
EditorGUILayout.FloatField("Dive Speed", state.DiveSpeed);
EditorGUILayout.FloatField("Min Dive Angle", state.MinDiveAngle);
}
EditorGUI.indentLevel--;
if(GUI.changed)
{
EditorUtility.SetDirty(MoveStates);
EditorUtility.SetDirty(target);
}
}
if(GUILayout.Button("Add")) MoveStates.AddMoveState(MoveStates.GetSize());
i = 0;
}
}
The MoveStateList:
using UnityEngine;
using UnityEditor;
using System;
public class MoveStateListAsset
{
[MenuItem("Assets/Create/MoveStates")]
public static void CreateAsset ()
{
ScriptableObjectUtility.CreateAsset<MoveStateList> ();
}
}
The Container Class:
using System;
[System.Serializable]
public class BaseMoveState
{
private static int CurrentID = 0;
private int ID = -1;
public string Name = "NewMoveState";
public float Acceleration = 0f;
public float Decceleration = 0f;
public float TurnSpeed = 0f;
public float BankSpeed = 0f;
public float MaxBankAngle = 0f;
public float ClimbSpeed = 0f;
public float MaxClimbAngle = 0f;
public float DiveSpeed = 0f;
public float MinDiveAngle = 0f;
public BaseMoveState BoostMoveState = null;
}
Answer by whydoidoit · Mar 14, 2013 at 02:39 PM
You need to do this kind of thing:
state.Name = EditorGUILayout.TextField("Name", state.Name);
The return value is the updated one...
Yup I just figured that out right before this finally posted. Thanks for the response - for some reason I was thinking too much in C++ and that state.Name was a pointer that would get modified directly.
please post comments AS CO$$anonymous$$$$anonymous$$ENTS cheers
btw. You should remove the "CanEdit$$anonymous$$ultipleObjects" attribute because you implemented a single object editor. To edit multiple objects you should use the SerializedObject ins$$anonymous$$d of "target". Editing multiple objects can be a bit tricky, so if you don't need this feature you should just removed the attribute.