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()));
}
}