Search Unity

Custom editor - scriptable object not saving data

Discussion in 'Scripting' started by ddule, Nov 12, 2018.

  1. ddule

    ddule

    Joined:
    Sep 9, 2017
    Posts:
    4
    Code (CSharp):
    1.      using System.Collections;
    2.      using System.Collections.Generic;
    3.      using UnityEngine;
    4.      using UnityEditor;
    5.      using System;
    6.      using UnityEditor.SceneManagement;
    7.    
    8.      [System.Serializable]
    9.      [CustomEditor(typeof(Wave))]
    10.      public class WaveEditor : Editor {
    11.    
    12.    
    13.    
    14.    
    15.          int numberOfSegments = 1;
    16.    
    17.          public override void OnInspectorGUI()
    18.          {
    19.              Wave myTarget = (Wave)target;
    20.          
    21.    
    22.    
    23.              EditorGUI.BeginChangeCheck();
    24.              numberOfSegments = EditorGUILayout.IntField("Number of wave segments:", numberOfSegments);
    25.              if (EditorGUI.EndChangeCheck())
    26.              {
    27.                  if (numberOfSegments < 1)
    28.                      numberOfSegments = 1;
    29.                  Array.Resize(ref myTarget.waveSegments, numberOfSegments);
    30.                  Array.Resize(ref myTarget.waveSegmentsDelay, numberOfSegments - 1);
    31.    
    32.              }
    33.    
    34.            
    35.    
    36.    
    37.    
    38.              for (int i = 0; i < myTarget.waveSegments.Length; i++)
    39.              {
    40.                  myTarget.waveSegments[i]=(WaveSegment)EditorGUILayout.ObjectField(myTarget.waveSegments[i], typeof(WaveSegment), true);
    41.                  if (i != myTarget.waveSegments.Length - 1) myTarget.waveSegmentsDelay[i] = EditorGUILayout.FloatField("Delay", myTarget.waveSegmentsDelay[i]);
    42.              }
    43.    
    44.              if (GUI.changed)
    45.              {
    46.                  Undo.RecordObject(myTarget,"save");
    47.                  EditorUtility.SetDirty(myTarget);
    48.                  EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    49.                  AssetDatabase.SaveAssets();
    50.                  Repaint();
    51.                  Debug.Log("SAVED");
    52.              }
    53.    
    54.    
    55.          
    56.              
    57.          }
    58.    
    59.      }
    60.    
    61.      using System.Collections;
    62.      using System.Collections.Generic;
    63.      using UnityEngine;
    64.      using UnityEditor;
    65.      [System.Serializable]
    66.      [CreateAssetMenu(fileName = "Wave", menuName = "Wave/Wave", order = 3)]
    67.      public class Wave : ScriptableObject
    68.      {
    69.    
    70.          public WaveSegment[] waveSegments;
    71.          public float[] waveSegmentsDelay;
    72.    
    73.    
    74.          private void Awake()
    75.          {
    76.              waveSegments = new WaveSegment[1];
    77.          
    78.          }
    79.    
    80.      }
    81.    
    82.      
    i tried everything but still not saving...
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    In custom editors you should try to only modify SerializedProperties, doing stuff directly on the target gets into the twilight zone. If you do SerializedProperties a bunch of stuff just works.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You need to set a scriptable object as dirty, not the scene. ScriptableObjects are never part of the scene actually.

    Try using:
    Code (CSharp):
    1. EditorUtility.SetDirty(YourScriptableObjectRef);
    Instead of MarkSceneDirty.

    Plus, SO's are saved once the project saves. Ctrl->S / Save Project;
    Plus, do serializedObject.Update() at the beggining of the OnInspectorGUI & serializedObject.ApplyModifiedProperties() at the end of the method otherwise inspector won't update any value passed.

    Also, note that in an actual "Wave" SO you're wiping data in .Awake(). Don't do that.
     
    thquan2k3, wpetillo, jjbish and 6 others like this.
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Thanks, this helped me. I don't understand why these statements aren't in the instructions on how to use SettingsProvider: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/SettingsProvider.html

    Without those two calls, none of the changes I made in the Project Settings screen would persist to the scriptable object.