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

EditorUtility.SetDirty not saving TimeSpan on ScriptableObject

Discussion in 'Editor & General Support' started by Nyyus, Aug 21, 2020.

  1. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    I have a problem with my custom editor on a ScriptableObject.
    When i edit the TimeSpan via my custom editor it doesn't save / i can't save it (Ctr + S doesn't do anything), meaning recompiling scripts, pressing start and closing + reopening of the project reverts its values back to the default.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. [CreateAssetMenu()]
    4. public class MySObj : ScriptableObject
    5. {
    6.     public TimeSpan timeSpan;
    7. }
    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. [CustomEditor(typeof(MySObj))]
    4. public class MySObjEditor : Editor
    5. {
    6.     public override void OnInspectorGUI()
    7.     {
    8.         var mySObj = (MySObj)target; ;
    9.         var timespan = TimeSpan.Zero;
    10.         EditorGUI.BeginChangeCheck();
    11.         timespan = timespan.AddDays(EditorGUILayout.IntField("Days", mySObj.timeSpan.Days));
    12.         timespan = timespan.AddHours(EditorGUILayout.IntField("Hours", mySObj.timeSpan.Hours));
    13.         timespan = timespan.AddMinutes(EditorGUILayout.IntField("Minutes", mySObj.timeSpan.Minutes));
    14.         timespan = timespan.AddSeconds(EditorGUILayout.IntField("Seconds", mySObj.timeSpan.Seconds));
    15.         if (EditorGUI.EndChangeCheck())
    16.         {
    17.             mySObj.timeSpan = timespan;
    18.             EditorUtility.SetDirty(mySObj);
    19.             AssetDatabase.SaveAssets();
    20.         }
    21.     }
    22. }
    I have tryed many things from other threads [Undo.RecordObject, EditorUtility.SetDirty, AssetDatabase.SaveAssets, AssetDatabase.Refresh etc.] but none of them worked in any combination i tryed.
    I checked if EditorUtility.SetDirty sets the Obj to dirty by EditorUtility.IsDirty and it did.
    Yes I know it would be easy to add a variable to MySObj like a double for hours and make the timespan a getter, but i would like it to leave MySObj as it is.

    [not important for my problem]
    The TimeSpan.Add functions are my extension functions:
    Code (CSharp):
    1. public static class Ext
    2. {
    3.     public static TimeSpan AddDays(this TimeSpan timeSpan, int days) => timeSpan.AddTicks(days * TimeSpan.TicksPerDay);
    4.     public static TimeSpan AddHours(this TimeSpan timeSpan, int hours) => timeSpan.AddTicks(hours * TimeSpan.TicksPerHour);
    5.     public static TimeSpan AddMinutes(this TimeSpan timeSpan, int minutes) => timeSpan.AddTicks(minutes * TimeSpan.TicksPerMinute);
    6.     public static TimeSpan AddSeconds(this TimeSpan timeSpan, int seconds) => timeSpan.AddTicks(seconds * TimeSpan.TicksPerSecond);
    7.     public static TimeSpan AddMilliseconds(this TimeSpan timeSpan, int millisecond) => timeSpan.AddTicks(millisecond * TimeSpan.TicksPerMillisecond);
    8.     public static TimeSpan AddTicks(this TimeSpan timeSpan, long ticks) => timeSpan.Add(new TimeSpan(ticks));
    9. }
     
  2. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    Update: while looking throw the ScriptableObject File I've noticed that there was no difference between having a TimeSpan and not having one. Does this mean that a ScriptableObject can't "save" structs or non serialized classes?