- Home /
Why is Unity updating these values in asset file?
I am trying to create a custom asset which is stored in the Resources folder. Editing is fine and all but my problem comes in when testing. I run my little sample, change the values in the runtime GUI, and then stop the player.
When I click on the asset to look at it in the inspector (with debug on) I can see that not only the public vars but also the property and private one where saved into my asset file. In the custom asset's editor window I can also see the values where updated for the two public fields.
Is Unity suppose to work like this and how can I prevent runtime changes being saved?
public class TheAsset : ScriptableObject
{
public string data1;
public string data2;
public string PropData { get; set; }
private string privateData;
public string GetPrivateData()
{
return privateData;
}
public void SetPrivateData(string s)
{
privateData = s;
}
}
public class AssetEditor: EditorWindow
{
private static TheAsset asset = null;
[MenuItem("Window/Custom Asset Test")]
public static void ShowDiaQEditor()
{
AssetEditor window = EditorWindow.GetWindow<AssetEditor>("TEST");
window.LoadAsset();
}
public void LoadAsset()
{
asset = (TheAsset)Resources.LoadAssetAtPath("Assets/Resources/testdata.asset", typeof(TheAsset));
if (asset == null)
{
asset = ScriptableObject.CreateInstance<TheAsset>();
AssetDatabase.CreateAsset(asset, "Assets/Resources/testdata.asset");
AssetDatabase.Refresh();
}
}
public void OnFocus()
{
if (asset == null) LoadAsset();
}
public void OnGUI()
{
asset.data1 = EditorGUILayout.TextField("Data 1", asset.data1);
asset.data2 = EditorGUILayout.TextField("Data 2", asset.data2);
if (GUI.changed)
{
EditorUtility.SetDirty(asset);
}
}
}
public class Test : MonoBehaviour
{
private TheAsset asset;
void Start()
{
asset = (TheAsset)Resources.Load("testdata");
}
void OnGUI()
{
if (asset == null) return;
GUI.Window(0, new Rect(Screen.width * 0.5f - 100, Screen.height * 0.5f - 125, 200, 250), Window, "Test");
}
void Window(int id)
{
GUILayout.Label("Data 1");
asset.data1 = GUILayout.TextField(asset.data1);
GUILayout.Label("Data 2");
asset.data2 = GUILayout.TextField(asset.data2);
GUILayout.Label("PropData");
asset.PropData = GUILayout.TextField(asset.PropData);
GUILayout.Label("PrivData");
asset.SetPrivateData(GUILayout.TextField(asset.GetPrivateData()));
}
}
I should add, when I close and open Unity I see, in the asset's inspector, that the values where restored to what they where when I last edited it with the asset's custom editor window. So the runtime changes are not saved, but also not "forgotten" when I stop play testing.
Answer by MrDude · Sep 04, 2013 at 11:59 AM
You need to instantiate your object after loading it. Anything you do to the asset before you instantiate it is applied to the source asset,not the instance and as such it will persist.
Thanks, this made me realise how stupid I were :p
I should have done ... asset = (TheAsset)Instantiate(Resources.Load("testdata"));
Your answer
Follow this Question
Related Questions
How to load ScriptableObject from asset file immediately? 1 Answer
No solution in Monodevelop 5 Answers
Scriptable object keeps referencing the objects? 1 Answer
Using ScriptableObjects as Types/Enums 2 Answers
Override asset on import 0 Answers