I’m writing a custom editor for my scene transition portals, so that I can select the destination scene from a dropdown list which is populated with the names of all scenes available in the build editor, rather than relying on a string field for scene name (or integer for build index).
I’ve got the dropdown list working, but for some reason the values that I choose from the lists (both for this and for the target portal identifier enum) don’t seem to be being saved correctly, and are forgotten as soon as I press Play or change scenes.
I’ve tried using serializedObject.Update()
and serializedObject.ApplyModifiedProperties()
, with no luck. I’ve tried using Undo.RecordObject(portal)
, which a) only works if you actively save the scene before you press Play and b) only seems to work for the target scene field, but not for the enum values.
Any other suggestions?
My custom editor code is below:
[CustomEditor(typeof(Portal))]
public class PortalInspector : Editor
{
private Portal portal;
private string[] scenes;
private int sceneIndex = 0;
private void OnEnable() {
portal = (Portal)target;
scenes = GetSceneNames();
}
public override void OnInspectorGUI() {
//serializedObject.Update();
Undo.RecordObject(portal, "changes to Portal");
EditorGUILayout.LabelField("Identifier", EditorStyles.boldLabel);
portal.identifier = (Portal.PortalIdentifier)EditorGUILayout.EnumPopup("Identifier", portal.identifier);
EditorGUILayout.LabelField("Destination", EditorStyles.boldLabel);
sceneIndex = EditorGUILayout.Popup("Target Scene", sceneIndex, scenes);
portal.sceneToLoad = scenes[sceneIndex];
portal.targetPortal = (Portal.PortalIdentifier)EditorGUILayout.EnumPopup("Target Identifier", portal.targetPortal);
//serializedObject.ApplyModifiedProperties();
}
private string[] GetSceneNames() {
//This populates a string array with the names of the scenes from the Build Settings
//It also initialises sceneIndex with the index of the saved sceneToLoad
}
}
My Portal class has the following interface (i.e. the fields are public, so I don’t know why they aren’t being serialized):
public class Portal : MonoBehaviour
{
public enum PortalIdentifier { None, A, B, C }
public PortalIdentifier identifier = PortalIdentifier.A;
public string sceneToLoad = "";
public PortalIdentifier targetPortal = PortalIdentifier.None;
//Triggering code etc...
}