Search Unity

Resolved How do you save variable values in custom Editors?

Discussion in 'Scripting' started by thenazgulhimself, May 13, 2022.

  1. thenazgulhimself

    thenazgulhimself

    Joined:
    Jun 30, 2021
    Posts:
    22
    Whenever I set them in the inspector, run the game and stop running it, they just reset to nothing. Google told me that using SetDirty() would save them, but it didn't work. Tried using serializedObject, but then the script just wouldn't compile at all. Any idea what might be the problem?


    Code (CSharp):
    1. [CustomEditor(typeof(Cursor))]
    2. class CursorEditor : Editor
    3. {
    4.    public override void OnInspectorGUI()
    5.    {
    6.        base.OnInspectorGUI();
    7.  
    8.        Cursor cursor = (Cursor)target;
    9.        if (cursor == null) return;
    10.  
    11.        EditorUtility.SetDirty(target);
    12.  
    13.        cursor.playerCenter = (Transform)EditorGUILayout.ObjectField("Player Center", cursor.playerCenter, typeof(Transform), true);
    14.        cursor.target = (Transform)EditorGUILayout.ObjectField("Target", cursor.target, typeof(Transform), true);
    15.  
    16.        cursor.clampMode = (ClampModes)EditorGUILayout.EnumPopup("Clamp Mode", cursor.clampMode);
    17.  
    18.        if (cursor.clampMode == ClampModes.rectangle)
    19.        {
    20.            cursor.clampThreshold = EditorGUILayout.Vector2Field("Clamp Threshold", cursor.clampThreshold);
    21.        }
    22.        else if (cursor.clampMode == ClampModes.circle)
    23.        {
    24.            cursor.clampRadius = EditorGUILayout.FloatField("Clamp Radius", cursor.clampRadius);
    25.        }
    26.    }
    27. }
     
    Last edited: May 14, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
  3. thenazgulhimself

    thenazgulhimself

    Joined:
    Jun 30, 2021
    Posts:
    22
    That's the thing, neither of them work for some reason. I tried putting them both before and after the actions, but it did nothing. Thank you for the code thing btw.
     
    Last edited: May 14, 2022
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    There's a built-in type called
    Cursor
    in Unity... is that causing some confusion?
     
  5. thenazgulhimself

    thenazgulhimself

    Joined:
    Jun 30, 2021
    Posts:
    22
    Nope, it was a good idea to rename it to remove future confusion, but this didn't solve this problem.
     
  6. thenazgulhimself

    thenazgulhimself

    Joined:
    Jun 30, 2021
    Posts:
    22
    Ok, update. I put the editor class INSIDE the original class, so that it could access the private variables. Moved it outside of the class, changed the variables to be public, looks like that fixed the problem. Now I just need to find out how to access private variables again.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    What about decorating the private variables with [SerializeField] attributes? That's sort of the minimum for Unity serialize stuff.
     
  8. thenazgulhimself

    thenazgulhimself

    Joined:
    Jun 30, 2021
    Posts:
    22
    Yeah, I initially wanted to make everything using the target method, thinking that the serializedobject method was weird, but in the end I was just overthinking things and the result is pretty clean and it works. It even lets you add hints to fields, which is cool. Wish I knew how to close threads as resolved on this forum, the UI here is very confusing. Well, thank you for your time!
    Code (CSharp):
    1. [CustomEditor(typeof(CustomCursor)), CanEditMultipleObjects]
    2. class CustomCursorEditor : Editor
    3. {
    4.     private SerializedProperty _playerOriginPoint;
    5.     private SerializedProperty _target;
    6.     private SerializedProperty _mousePositionInputSO;
    7.     private SerializedProperty _clampMode;
    8.     private SerializedProperty _clampThreshold;
    9.     private SerializedProperty _clampRadius;
    10.  
    11.     private void OnEnable()
    12.     {
    13.         GetSerializedProperties();
    14.     }
    15.  
    16.     public override void OnInspectorGUI()
    17.     {
    18.         serializedObject.Update();
    19.  
    20.         DrawGUI();
    21.  
    22.         serializedObject.ApplyModifiedProperties();
    23.     }
    24.  
    25.     private void GetSerializedProperties()
    26.     {
    27.         _playerOriginPoint = serializedObject.FindProperty("_playerOriginPoint");
    28.         _target = serializedObject.FindProperty("_target");
    29.         _mousePositionInputSO = serializedObject.FindProperty("_mousePositionInputSO");
    30.         _clampMode = serializedObject.FindProperty("_clampMode");
    31.         _clampThreshold = serializedObject.FindProperty("_clampThreshold");
    32.         _clampRadius = serializedObject.FindProperty("_clampRadius");
    33.     }
    34.  
    35.     private void DrawGUI()
    36.     {
    37.         EditorGUILayout.ObjectField(_playerOriginPoint, new GUIContent("Player Origin Point", "The center around which the cursor can move."));
    38.         EditorGUILayout.ObjectField(_target, new GUIContent("Camera Target", "The point at which the camera is looking."));
    39.         EditorGUILayout.ObjectField(_mousePositionInputSO, new GUIContent("Mouse Pos SO", "The scriptable object of the mouse's current position."));
    40.  
    41.         EditorGUILayout.PropertyField(_clampMode, new GUIContent("Clamp Mode", "The way in which the camera target is clamped."));
    42.  
    43.         if (_clampMode.intValue == (int)ClampModes.rectangle)
    44.         {
    45.             EditorGUILayout.PropertyField(_clampThreshold, new GUIContent("Clamp Threshold", "Size of the rectangular border of the target."));
    46.         }
    47.         else if (_clampMode.intValue == (int)ClampModes.circle)
    48.         {
    49.             EditorGUILayout.PropertyField(_clampRadius, new GUIContent("Clamp Radius", "Radius of the circular border of the target."));
    50.         }
    51.     }
    52. }
     
    Kurt-Dekker likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    I think you just edit your post and there's a RESOLVED flair... or maybe redo the subject line with [Resolved]?

    Either way, coming back like you did to summarize what you did is awesome and how the forum steadily becomes a bigger repository of experience and knowledge.