I have a ScriptableObject reference in a MonoBehaviour class.
Modify its values with CustomEditor button.
Save It.
Values seems changed.
Even selected ScriptableObject Inspector displays data modified.
But asset file on disk don't have any data.
And there's no data available from code of any another MonoBehaviour instance.
Here is a code snippet used to save my ScriptableObject.
public TextAsset text;
[SerializeField]
//Thats My Scriptable Object
public PathFramesList pathFramesList;
[SerializeField]
public List<List<ICurveData>> frameDatasList = new List<List<ICurveData>>();
// Called By CustomEditor Button
public void ReadData()
{
#region BlahBlahBlah generating some data to save
var separator = new string[] { "%" };
var framesUnparsedData = text.text.Split(separator, StringSplitOptions.None);
foreach (var f in framesUnparsedData)
frameDatasList.Add(ParseFrame(f));
var result = new ICurveData[frameDatasList.Count][];
for (int i = 0; i < frameDatasList.Count; i++)
result[i] = frameDatasList[i].ToArray();
#endregion
#region Trying to save data
pathFramesList.frameDatasList = result; // Set data to object
EditorUtility.SetDirty(pathFramesList); // Even This didn't help
AssetDatabase.SaveAssets(); // Looks it doesn't work
AssetDatabase.Refresh(); // Hoped this will help, whatever it do
#endregion
}
And that's my ScriptableObjectClass
And Its CustomEditor
[CreateAssetMenu(fileName = "ParsedPathData", menuName = "Create Path Data Asset")]
[System.Serializable]
public class PathFramesList : ScriptableObject
{
[SerializeField]
public ICurveData[][] frameDatasList;
}
#if UNITY_EDITOR
[CustomEditor(typeof(PathFramesList))]
public class PathFramesListEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
PathFramesList pathFrameList = (PathFramesList)target;
if(pathFrameList.frameDatasList == null)
{
GUILayout.TextField("Frames Count is 0");
}
else
{
// It still has all data
GUILayout.TextField("Frames Count is " + pathFrameList.frameDatasList.Length, 200);
}
if(GUILayout.Button("SaveAsset"))
{
// Tried to force saving with this) Didn't help
EditorUtility.SetDirty(target);
EditorUtility.SetDirty(serializedObject.targetObject);
serializedObject.Update();
serializedObject.ApplyModifiedProperties();
AssetDatabase.SaveAssets();
}
}
}
#endif
question from:
https://stackoverflow.com/questions/65871548/scriptableobject-data-modified-from-code-in-editor-mode-but-containing-asset-fi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…