- Home /
Change contents of TextAsset
I'm writing a custom editor that I want to edit TextAsset contents. What I have right now is a reference to a TextAsset, and what I would love to do is edit textAsset.text, and then call EditorUtility.SetDirty(textAsset) to write the changes to disk. Unfortunately, testAsset.text is readonly. I can't figure out a way to convert from a TextAsset to a system file path, either. Does anyone know how to do this? Any way that I could figure out the local file path of an object selected in the Project panel would be helpful as well.
Thanks!
Answer by Shawn · Feb 24, 2010 at 07:04 PM
I was not 100% clear if you were talking about editing the text asset from inside the editor or not. The below information is for modifying files on a run-time build (I did see that your tags included the editor)...
As far as I know the TextAssets are compiled into the same assets files as your other content; which means once the application is deployed it would be compiled into something that looks like this "sharedassets0.assets". There would be no easy way to change the text asset directly without some clever file manipulation.
An easier option would be to maybe store your text assets in another directory local to your project and instead of visually linking them up as assets you would just use the .NET IO API to load the text dynamically and then you could easily save any text changes you needed back to that file. This solution would prevent you from visibly linking them in the editor, but if you needed to assign them in the editor the easiest way would be to just have a string field where you put the file name for the text file and then your application would use that filename when loading your file from code.
If you are attempting to do this in the editor and would like to get the editor path to the raw text asset you can use the AssetDatabase.GetAssetPath() method:
http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.GetAssetPath.html
AssetDatabase.GetAssetPath() is exactly what I needed. Thanks! Yes, this is for in-editor.
Answer by Andy Lee · Apr 14, 2016 at 12:17 PM
Though it has been late, but I want to share a quick solution for this question:
File.WriteAllText(AssetDatabase.GetAssetPath(TEXT_ASSET), STRING);
EditorUtility.SetDirty(TEXT_ASSET);
It should do the trick. Remember to add using System.IO;