I have sctiptable object
var dialogueContainer = ScriptableObject.CreateInstance<DialogueContainer>();
I save the object to resources folder by code
AssetDatabase.CreateAsset(dialogueContainer, $"Assets/Resources/{fileName}.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
If the object previously had been created then it get overwrited.
But if the object is selected in Project tab , I can not see any changes unless I go to the other folder and then return back and select object.
How to avoid switching folder on rewrite existed asset?
What do you mean by this? Unity will not commit the changes to the asset file to disk until you do a Save Project… this is infuriating in the year 2023 but is simply how Unity works.
I already know that there is an issue if i change the existent asset
well even run the game, after i made changes to the scriptable asset file (pressed Save and Save Project), shows me the data of scriptable object before save.
And if I want to see the changes in play mode, I need to quit unity and start it again.
I believe there should be some saving methods for that.
I actually have another scriptable object(first) the has field and in that field that scriptable object(second) where I am making changes via code.
When I save changes to asset by code like that :
EditorUtility.SetDirty(dialogueContainer);
AssetDatabase.CreateAsset(dialogueContainer, assetName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
The first scriptable object is loosing link to the second scriptable object and I need either to relink second object or restart unity.
Any ideas how to fix it?
Order is critically and silently important with the asset database.
If you:
- create something
- put a reference to it in another asset
- write the something to disk
It will not work. The asset will NOT have a reference.
You must:
- create something
- write the something to disk
- THEN put a reference to it in the other asset
And in all cases, dirty the asset you updated.
ok, I found the solution for both questions. Here is the code:
var assetName = $"Assets/Resources/{fileName}.asset";
if (File.Exists(assetName))
{
var theFile = Resources.Load<DialogueContainer>(fileName);
EditorUtility.CopySerialized(dialogueContainer, theFile);
theFile.name = Path.GetFileName(fileName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"Dialogue <color=yellow>{fileName}</color> <color=green>Refreshed</color>. {DateTime.Now}");
}
Bing chat dealed with the problem better than ChatGPT this time.