Search Unity

Can't make Custom Inspector to "save settings"

Discussion in 'Scripting' started by MrBIoBR, Jul 2, 2017.

  1. MrBIoBR

    MrBIoBR

    Joined:
    Jul 2, 2017
    Posts:
    9
    Hi there guys.
    i'm trying to make a custom inspector ( for a sound script ) and i'm trying for days to make it work properly @__@
    the thing is, all the settings that i apply in edit mode ( like, checking some boxes ) simply desapear when i hit play or change to another game object.

    i've search around and found a bunch of people with this problem, and i'm trying to solve it by making the things i've read but apparently i'm not doing correctly because, wel, still not working :( here is a piece of the editor:
    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEditor;
    7. using UnityEditor.SceneManagement;
    8.  
    9. [CustomEditor(typeof(SoundBoxEdited))]
    10. //[ExecuteInEditMode]
    11.  
    12. public class SoundBoxEditor : Editor {
    13.  
    14.     [SerializeField]
    15.     bool activateOnStart = false;
    16.     SoundBoxEdited SB;
    17.  
    18.     public override void OnInspectorGUI()
    19.     {
    20.         SB = (SoundBoxEdited)target;
    21.         EditorGUI.BeginChangeCheck();
    22.                                                                                                  
    23.     activateOnStart = EditorGUILayout.ToggleLeft("Activate On Start ", activateOnStart);
    24.  
    25.             if (activateOnStart)
    26.             {
    27.                 SB.activateOnStart = true;          
    28.                 randomizeStartDelay = EditorGUILayout.ToggleLeft("RandomizeDelay", randomizeStartDelay);
    29.  
    30.                 if (randomizeStartDelay)
    31.                 {
    32.                     SB.startDelayMin = EditorGUILayout.FloatField("minDelay", SB.startDelayMin);
    33.                     SB.startDelayMax = EditorGUILayout.FloatField("maxDelay", SB.startDelayMax);            
    34.                 }
    35.                 else
    36.                     SB.startDelay = EditorGUILayout.FloatField("StartDelay", SB.startDelay);
    37.             }
    38.             else
    39.                 SB.activateOnStart = false;
    40.  
    41.         if (EditorGUI.EndChangeCheck())
    42.         {
    43.             EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    44.         }
    45. }
    46.  
    Can anyone help me ? i will be glad...
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    first off you don't need [ExecuteInEditMode], [SerializeField], or the SceneManagement. ExecuteInEditMode is meant for monobehaviours and SerializeFields is meant for fields in classes you want the Editor to draw, like Monobehaviour,ScriptableObjects, and custom classes. Don't rely on GetActiveScene, its unreliable when you use it in multiscene setups, plus you don't need to mark the scene dirty in the first place, just the object itself.

    use SerializedObject and SerializedProperty instead.
    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. [CustomEditor(typeof(SoundBoxEdited))]
    4. public class SoundBoxEditor : Editor
    5. {
    6.     public override void OnInspectorGUI()
    7.     {
    8.         serializedObject.Update();
    9.  
    10.         SerializedProperty p_activateOnStart = serializedObject.FindProperty("activateOnStart");
    11.         SerializedProperty p_randomizeStartDelay = serializedObject.FindProperty("randomizeStartDelay");
    12.         SerializedProperty p_startDelayMin = serializedObject.FindProperty("startDelayMin");
    13.         SerializedProperty p_startDelay    = serializedObject.FindProperty("startDelay");
    14.         SerializedProperty p_startDelayMax = serializedObject.FindProperty("startDelayMax");
    15.  
    16.         bool activateOnStart= EditorGUILayout.ToggleLeft("Activate On Start ", p_activateOnStart.boolValue);
    17.  
    18.         if(activateOnStart)
    19.         {
    20.             bool randomizeStartDelay = EditorGUILayout.ToggleLeft("Randomize Delay", p_randomizeStartDelay.boolValue);
    21.             if(randomizeStartDelay)
    22.             {
    23.                 EditorGUILayout.PropertyField(p_startDelayMin);
    24.                 EditorGUILayout.PropertyField(p_startDelayMax);
    25.             }
    26.             else
    27.             {
    28.                 EditorGUILayout.PropertyField(p_startDelay);
    29.             }
    30.  
    31.             p_randomizeStartDelay.boolValue = randomizeStartDelay;
    32.         }
    33.  
    34.         if(EditorGUI.EndChangeCheck())
    35.         {
    36.             serializedObject.ApplyModifiedProperties();
    37.         }
    38.  
    39. }
    ApplyModifiedProperties will mark the object dirty which in turn triggers Unity to write the changes to file.

    Also you're code didn't mention where you've defined randomizeStartDelay. but you'll want that to be a field on the object so that the changes are saved.
     
  3. MrBIoBR

    MrBIoBR

    Joined:
    Jul 2, 2017
    Posts:
    9
    Thanks man, it works !
    i'm going to read a more about serializedObject and SerializedProperty so i understand more about it.
    thanks.