- Home /
Force Unity to refresh a text asset (Resources folder)
Hello everybody,
I have created a small editor script that creates text files in my Resources folder.
I then noticed that after creating these files, Unity does not always refresh its memory of their content.
So after some research, I have found out about SetDirty, and I am using it like this on every file that I save in the Resources folder:
static void SetDirty( string path ) {
TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath( path, typeof(TextAsset) );
EditorUtility.SetDirty( textAsset );
}
This helps things a little, but still in order for Unity to re-import these assets and their refreshed content, I need to alt-tab away from Unity, and back in.
Am I doing anything wrong, have I missed something, or is this just the way it works?
Thanks.
Answer by Veehmot · Jun 10, 2013 at 08:02 PM
As you found out, AssetDatabase.Refresh is the right choice when you need to reimport everything; that's what Unity does when you edit a file.
But if you want to refresh a single file, you should use AssetDatabase.ImportAsset.
Thanks. Also helpful indeed. I wonder if I can skip SetDirty if I use ImportAsset? EDIT: Looks like there is no need for my SetDirty call, so this answer is better.
The way you are using it, yes. SetDirty tells the engine you did a change to an asset so it should be saved. You are marking the asset as dirty right after you import, but you should mark it if you ever do a change to the asset using scripting.
But being a TextAsset, there's no need at all to use SetDirty, you can freely edit your text outside Unity and reload it using Refresh.
Answer by DannyB · Jun 10, 2013 at 07:30 PM
Found the answer: AssetDatabase.Refresh() seems to make everything right.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103918im_/https://answers.unity.com/themes/thub/images/avi.jpg)