Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by BimSekai · Aug 28, 2012 at 10:26 AM · filetextassettextfile

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 ? :/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BimSekai · Aug 28, 2012 at 11:43 AM 0
Share

Textasset.text is read only :(

avatar image Kryptos · Aug 28, 2012 at 11:45 AM 0
Share

Damned! This is unfortunate. I updated my answer (I keep the original answer so that anyone can see the mistake and learn from it).

avatar image BimSekai · Aug 28, 2012 at 12:12 PM 0
Share

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 ?

avatar image tayl0r · Nov 27, 2012 at 06:02 AM 0
Share

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.

avatar image Kryptos · Nov 27, 2012 at 08:52 AM 1
Share

@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.

Show more comments
avatar image
12

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();
         
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image s_lheureux · Sep 29, 2014 at 10:33 PM 0
Share

Just as a clarification, the file extension needs to be .txt and not .text as it's written in this answer.

avatar image dush88c · Jan 11, 2019 at 10:38 AM 0
Share

Application.persistentDataPath is the correct path to give to save a file

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Aug 24, 2015 at 05:36 AM 0
Share

Ok you figured out how to use File methods but stop posting them on all answers, particularly when the answer is already answered...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using OpenFilePanel at runtime to open a file as a TextAsset to read data is giving NullReferenceException 1 Answer

Text file read, random quote C# 1 Answer

How does a TextAsset work? 1 Answer

Resources.Load not giving me all data for a .txt file 1 Answer

Reading Data generated in Processing from a TextAsset 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges