Search Unity

Objects in Custom Editor Resetting on Play

Discussion in 'Scripting' started by jcwhite_nc, May 7, 2021.

  1. jcwhite_nc

    jcwhite_nc

    Joined:
    Jan 28, 2020
    Posts:
    1
    I'm trying to have a list of steps shown in the custom editor and then updating the serialized Object with some values. Whenever I press play the StepRestriction.referenceStep and StepRestriction.stepIndex are reset to the first item in the steps list and 0. I've looked on previous forums and the general response is to add serializedObject.Update() and serializedObject.ApplyModifiedProperties(), but I have both of these. I'm lost on why they keep resetting.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. namespace TutorialSystem
    6. {
    7. ...
    8.     public class StepRestriction : MonoBehaviour
    9.     {
    10.         [SerializeField]
    11.         RestrictionType restrictionType;
    12.         [SerializeField]
    13.         TutorialManager tutorialManager;
    14.  
    15.         [SerializeField]
    16.         TutorialStep referenceStep;
    17.  
    18.         [SerializeField]
    19.         public int stepIndex;
    20.  
    21.         [SerializeField]
    22.         internal List<RestrictionPair> restrictions = new List<RestrictionPair>();
    23.  
    24. ...
    25.     }
    26.  
    27. #if UNITY_EDITOR
    28.     [CustomEditor(typeof(StepRestriction))]
    29.     public class StepRestrictionInspector : Editor
    30.     {
    31.         SerializedProperty p_restrictionType;
    32.         SerializedProperty p_tutorialManager;
    33.         SerializedProperty p_stepIndex;
    34.         SerializedProperty p_onStep;
    35.         SerializedProperty p_referenceStep;
    36.  
    37.         int selectedStepIndex;
    38.  
    39.         private void OnEnable()
    40.         {
    41.             p_restrictionType = serializedObject.FindProperty("restrictionType");
    42.             p_tutorialManager = serializedObject.FindProperty("tutorialManager");
    43.             p_stepIndex = serializedObject.FindProperty("stepIndex");
    44.             p_onStep = serializedObject.FindProperty("onStep");
    45.             p_referenceStep = serializedObject.FindProperty("referenceStep");
    46.         }
    47.  
    48.         public override void OnInspectorGUI()
    49.         {
    50.             serializedObject.Update();
    51. ...
    52.             EditorGUILayout.PropertyField(p_tutorialManager);
    53.             ShowSteps();
    54.             ShowRestrictions();
    55.  
    56.             serializedObject.ApplyModifiedProperties();
    57.         }
    58. ...
    59.         private void ShowSteps()
    60.         {
    61.             TutorialManager manager = (TutorialManager)p_tutorialManager.objectReferenceValue;
    62.             if (manager == null) return;
    63.  
    64.             TutorialStep[] steps = manager.GetSteps();
    65.  
    66.             //create the array for the step names
    67.             string[] stepStrings = new string[steps.Length];
    68.             for (int i = 0; i < steps.Length; i++)
    69.             {
    70.                 stepStrings[i] = steps[i].name;
    71.             }
    72.  
    73.             //set the step from the popup
    74.             selectedStepIndex = EditorGUILayout.Popup(new GUIContent("Step"), selectedStepIndex, stepStrings);
    75.             p_stepIndex.intValue = selectedStepIndex;
    76.             p_referenceStep.objectReferenceInstanceIDValue = steps[selectedStepIndex].GetInstanceID();
    77.         }
    78.     }
    79.  
    80. #endif
    81. }
     
  2. maax511

    maax511

    Joined:
    Jan 21, 2021
    Posts:
    6
    Hi, I've encountered same issue while working with EditorWondow. I had a bunch of windows deffered from some abstract class so they all had similar behaviour. When it came to testing I realized that all windows work fine but one specific window resets It's values at some point. I checked it out: no errors, no exceptions. Tried to debug it with breakpoints and it seemed to be working fine but it didn't. I even tried to check if this one of the known issues in Unity and developers of the engine are working to fix it but didn't found anything that looked similar.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,842
    It's normal for an editor window to reset it's values during a domain reload as it's values are not serialised out anywhere. It's just a scriptable object living in memory.

    But because editor windows are scriptable objects you can create an asset out of them and let the values live on disk properly.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    Please start your own thread. Your question isn't really related to a custom editor / inspector. Also if you have a specific issue, you should include more details in your post (especially relevant code).

    EditorWindows usually survive an assembly reload as long as the fields are serializable by Unity. Of course entering or leaving play mode can have some additional issues as objects in the active scene(s) may no longer exist after the play mode change.

    Anyways, as I said, don't try to hijack other threads, especially when they are that old.