Question by
unity_9_8SJhR79PmLNg · Apr 29, 2018 at 06:51 PM ·
variableserializationserializedpropertyeditor extension
Keep editor variable value
I've got a problem with creating custom editor for my script. Sript contains array of scriptable objects, whitch are holding a Vector3 value. I want to display each of the objects in the inspector and when I select one, rotation handle should appear in the scene view, so I can edit the value. So far I came with following solution
[CustomEditor(typeof(MyScript)), CanEditMultipleObjects]
public class MyScriptEditor : Editor {
SerializedProperty arrayOfScriplableObjects;
[SerializeField]
int selected = 0;
private void OnEnable()
{
arrayOfScriplableObjects= serializedObject.FindProperty("arrayOfScriplableObjects");
}
public override void OnInspectorGUI()
{
arrayOfScriplableObjects.Update();
for (int i = 0; i < arrayOfScriplableObjects.arraySize; i++) {
SerializedProperty entry = arrayOfScriplableObjects.GetArrayElementAtIndex(i);
GUI.SetNextControlName("Obj-" + i);
EditorGUILayout.PropertyField(entry, true);
}
if (GUI.GetNameOfFocusedControl().Contains("Obj"))
{
selected = int.Parse(GUI.GetNameOfFocusedControl().Split('-')[1]);
}
serializedObject.ApplyModifiedProperties();
}
protected virtual void OnSceneGUI()
{
//Selected is always 0
arrayOfScriplableObjects.GetArrayElementAtIndex(selected);
}
}
The problem is that variables set in OnInspectorGUI are reset when OnSceneGUI is called so I'm unable to do any operations with controls drawn in the scene view.
Comment
Your answer
