- Home /
Create a Textasset from a string
Hello :)
The goal of my editor script is to create a Textasset containing the string I give it. At the moment, I do the following: Create a .txt file in Assets/Resources containing the text Then make it a textasset using:
(TextAsset)Resources.LoadAssetAtPath(myfile,typeof(TextAsset))But it returns null if the Textfile is created above in the code, just like the textfile isn't finished yet (I closed the stream, I triplechecked this) However, it works if the textfile was here before the script started. (and the TextAsset.text has the value of the string in the text BEFORE the script is executed (I'm not sure if i'm clear in the explanation...)
Lets make an example: I want to write "ABC" in my asset.
if the file does not exists, my asset is null.
if the file exists and contains "123" after the execution of my script the TextAsset.text will be 123, but the file on my disk will have "ABC".
Is it clearer like this ? :/
Answer by Kryptos · Aug 28, 2012 at 10:53 AM
How did you create the text file?
You need to use AssetDatabase.CreateAsset and an object of type TextAsset.
Try something like:
TextAsset text = new TextAsset();
AssetDatabase.CreateAsset(text, "Assets/MyText.txt");
Also, don't forget to commit any change using AssetDatabase.SaveAssets.
text.text = "my text content";
AssetDatabase.SaveAssets();
edit: for some obscure reasons, TextAsset.text is read-only. The above code will not work.
In that case, try to refresh the database before creating the TextAsset, using AssetDatabase.Refresh. It is worth to note that Unity Editor only works with cached assets not with the real asset files on disk.
Damned! This is unfortunate. I updated my answer (I keep the original answer so that anyone can see the mistake and learn from it).
Worked like a charm, thanks !
WriteFile(); AssetDatabase.Refresh(); CreateBundleFromFile();
I think i'll just buy a poster of you and pin it in my bedroom, you're my new favorite super hero, you know that ?
Does this still work? I can't seem to write to the text property of the TextAsset even after trying the AssetDatabase.Refresh trick: Property or indexer `UnityEngine.TextAsset.text' cannot be assigned to (it is read only)
It seems like this is a compile time error anyways so I'm not how AssetDatabase.Refresh would fix anything in that case.
@tayl0r $$anonymous$$aybe the answer and comments are not very clear.
You cannot assign the read-only property. What you can do is write to the file, then call AssetDatabase.Refresh().
Note that this solution is for editor script only. At runtime there is no such thing as AssetDatabase.
Answer by Tomas1856 · Jan 24, 2013 at 04:37 PM
You're all thinking too hard. Just do:
File.WriteAllText(Application.dataPath + "/test.text", "blabla"));
AssetDatabase.Refresh();
Just as a clarification, the file extension needs to be .txt and not .text as it's written in this answer.
Application.persistentDataPath is the correct path to give to save a file
Answer by Skari · Apr 08, 2015 at 01:10 PM
TextAsset ConvertStringToTextAsset(string text) {
string temporaryTextFileName = "TemporaryTextFile";
File.WriteAllText(Application.dataPath + "/Resources/" + temporaryTextFileName + ".txt", text);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
TextAsset textAsset = Resources.Load(temporaryTextFileName) as TextAsset;
return textAsset;
}
This is a complete solution for how I tried to solve this. As mentioned in Kryptos's comment AssetDatabase can be used only in editor, not in builds. I asked for a solution for builds in #unity3d irc channel and was told that there is no way to create TextAssets on the fly in builds.
Answer by drudiverse · Aug 24, 2015 at 05:32 AM
in JS... THIS WORKS!:)
import System.IO;
function writeStuffToFile(){ var stuff : String = "stuff"; File.WriteAllText(Application.dataPath +"/stuff.txt",stuff); }
function readStufFromFile(){ var stuff : String; stuff = File.ReadAllText(Application.dataPath +"/stuff.txt"); Debug.Log(stuff); }
Ok you figured out how to use File methods but stop posting them on all answers, particularly when the answer is already answered...