Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Custom Inspector doesn't save Scriptable Object data when restarting Unity Project

Discussion in 'Scripting' started by WanAmir, Jul 11, 2023.

  1. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    Hi. I have a custom editor to Add or Remove FeedbackData in FeedbackDataList.
    The functions works well in Editor but when I restart the project the scriptable object data is empty.
    But when I tweak the value directly inside the scriptable object the data persist.

    Screenshot_1.png

    Screenshot_2.png

    These are my scripts.
    Code (CSharp):
    1. [CreateAssetMenu(fileName = "Feedback Data", menuName = "ETAS/Feedback")]
    2.     public class FeedbackDataSO : ScriptableObject
    3.     {
    4.         public Pose CollapsePose;
    5.         public Pose ExpandPose;
    6.         public List<FeedbackData> FeedbackDataList = new();
    7. }
    8.  
    9.     [System.Serializable]
    10.     public struct FeedbackData
    11.     {
    12.         public Pose Pose;
    13.         public float Duration;
    14.  
    15.         public FeedbackData(Vector3 pos, Quaternion rot, float duration)
    16.         {
    17.             Pose.position = pos;
    18.             Pose.rotation = rot;
    19.             Duration = duration;
    20.         }
    21.     }
    22.  
    23. [CustomEditor(typeof(StepControllerV2))]
    24.     public class StepControllerV2Editor : Editor
    25.     {
    26.         StepControllerV2 step = null;
    27.  
    28.         void OnEnable()
    29.         {
    30.             step = target as StepControllerV2;
    31.         }
    32.  
    33.         public override void OnInspectorGUI()
    34.         {
    35.             base.OnInspectorGUI();
    36.  
    37.             serializedObject.Update();
    38.  
    39.             DisplayGenerateStepAndFeedbackButton();
    40.  
    41.             EditorGUILayout.PropertyField(serializedObject.FindProperty("FeedbackData"), true);
    42.  
    43.             DisplayAddAndRemoveFeedbackButton();
    44.  
    45.             serializedObject.ApplyModifiedProperties();
    46. }
    47.  
    48. void AddFeedback ()
    49.         {
    50.             FeedbackData data = new FeedbackData(step.Position, step.Rotation, 1f);
    51.             step.FeedbackData.FeedbackDataList.Add(data);
    52.  
    53.             step.FeedbackData.SetExpandPose(step.Position, step.Rotation);
    54.  
    55.             AssetDatabase.SaveAssets();
    56.             AssetDatabase.Refresh();
    57.         }
    58.  
    59.         void RemoveFeedback()
    60.         {
    61.             if (step.FeedbackDataFeedbackDataList.Count > 0)
    62.             {
    63.                 int lastIndex = step.FeedbackData.FeedbackDataList.Count - 1;
    64.  
    65.                 FeedbackData data = step.FeedbackData.FeedbackDataList[lastIndex];
    66.  
    67.                 step.transform.SetPositionAndRotation(data.Pose.position, data.Pose.rotation);
    68.             }
    69.             else
    70.             {
    71.                 step.transform.SetPositionAndRotation(step.FeedbackData.CollapsePose.position, step.FeedbackData.CollapsePose.rotation);
    72.             }
    73.  
    74.             AssetDatabase.SaveAssets();
    75.             AssetDatabase.Refresh();
    76.         }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Whenever you modify an asset via code, you must inform Unity you have altered it. This is generally done via
    EditorUtility.SetDirty
    , but can also be done via recording the object via the
    Undo
    class.

    In your case you need to dirty the scriptable objects.
     
    Bunny83 and WanAmir like this.
  3. WanAmir

    WanAmir

    Joined:
    Jul 17, 2018
    Posts:
    27
    Thank you. It works now