- Home /
Modifying and Saving Scriptable Objects: SetDirty vs. Undo
Hi, I am having some trouble understanding how saving using Undo.* works. I currently have scriptable objects that I am using to pass data between scenes. I am setting some options in the main menu, and I want to use those options in the game. This is my current code to save the object every time it is modified.
[CreateAssetMenu]
public class IntVariable : ScriptableObject
{
[SerializeField] private int _variable;
public int Variable
{
get => _variable;
set
{
_variable = value;
// EditorUtility.SetDirty(this);
}
}
}
If I uncomment the SetDirty line, then it all works exactly how I want it to; however, looking online (https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html), SetDirty apparently shouldn't be used anymore, I assumed it was becoming deprecated. Instead of SetDirty, Unity says we are supposed to use Undo.RecordObject(). I tried doing that with this code instead
public class IntVariable : ScriptableObject
{
[SerializeField] private int _variable;
public int Variable
{
get => _variable;
set
{
Debug.Log(EditorUtility.IsDirty(this));
Undo.RecordObject(this, "Change int");
_variable = value;
// Undo.FlushUndoRecordObjects();
Debug.Log(EditorUtility.IsDirty(this));
}
}
}
With debugs to tell if it was actually dirty like it was supposed to be, and it both says it isn't dirty, and when I close and reopen the project, nothing indeed has been saved. Is there something we need to call in order to save these default values, or am I just using the syntax wrong, or am I just going about this whole thing in the wrong way?
Answer by Wappenull · Sep 22, 2020 at 02:46 PM
This could be answered in 2 completely different topics.
"SetDirty vs. Undo"
.
Undo.RecordObject is mostly for scene objects, as written in its document.
EditorUtility.SetDirty is for non-scene object. Without undo support. The sole purpose of SetDirty is for editor to believe that this object is dirty and really write it to disk.
If you edit your object via SerializedObject way you don't have to call any above. SerializedObject already handle everything. This also happen internally when you edit fields in inspector. Of course you cannot use SerializedObject in game runtime because it is in UnityEditor namespace.
"I am using (this) to pass data between scenes. I am setting some options in the main menu, and I want to use those options in the game."
.
Looks like you want to pass "game option" around. There is many way to do it. ScriptableObject is NOT one of these way!
.
ScriptableObject are mostly for "constant database" not for dynamic data passing. Like database of items, enemy stats, or for storing reference to another asset (UnityEngine.Object). Furthermore, ScriptableObject are only editable and savable in Editor. You wont be able to modify it when build a standalone game. So it is not suitable for your task at all.
The first and easiest approach is to use static variable. Make 1 global game state, write to it. Access it from anywhere even scene has changed.
If you also want to save them between each game launch, now you probably need PlayerPref.
Your answer
Follow this Question
Related Questions
How do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers
Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers
Changes to custom serializable object not saved to disk 1 Answer
How to Save and Load a list of Scriptable Objects from a File? 2 Answers