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 absameen · Sep 11, 2012 at 08:57 PM · jsontextassetresource

Load JSON Resource as TextAsset

Hello, I'm trying to load a text file that contains JSON data as a TextAsset. This works fine if the extension of .txt is used,

For example myJson.txt works with:

TextAsset t = (TextAsset) Resources.Load("myJson", typeof(TextAsset));

However, when the extension is changed to .json, the resource will not load. How can we load the file as a TextAsset despite the .json extension? Is there a better way to load text from a file on runtime?

Comment
Add comment · Show 3
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 sandeepsmartest · May 04, 2016 at 12:49 PM 0
Share

how to load the json/text file in android?application.datapath is not working!!any help?

avatar image Bunny83 sandeepsmartest · May 04, 2016 at 01:07 PM 0
Share

@sandeepsmartest:

That's completely off topic. Resources are built into the game and can only be gathered through the Resources class. The resources don't exist as seperate files in the built game but are usually packed into the game data. A TextAsset is a special kind of asset that stores the data of the file "as it is" without special handling. Unity recognises only certain extensions as TextAssets.

In the past only .txt / .bytes / .xml where supported. The current Unity version recognises more extensions. See the docs for more details (.json is now supported).

Reading plain files from the local filesystem can be done with the ordinary System.IO classes.

If you want to ship files with your build you can now use Strea$$anonymous$$gAssets. $$anonymous$$ake sure you read the information carefully. Strea$$anonymous$$gAssets work quite differently on different platforms. On Android those are still packed into the apk file. To read them you either have to use Unity's WWW class or a seperate ZIP framework with can access files in archives.

You can't overwrite / edit those files inside the AP$$anonymous$$. You might want to extract them on first start into the persistent data path if you need to change and save the content.

avatar image sandeepsmartest Bunny83 · May 04, 2016 at 01:33 PM 0
Share

Hei Bunny,Thanks for the immediate response.$$anonymous$$y requirement is to just read the txt file which has the content in json style.Yeah, I just sorted the issue by using Text Asset.Below given is the implementation that have used for Android.Point me in right direction if in case am going in wrong way of accessing the text file(Performance and GC wise)

     public void ReadFile( )
       {
         //TextAss declared as public variable and drag dropped the text file in inspector
             string content = TxtAss.text;
             print("NS$$anonymous$$ NS$$anonymous$$ NS$$anonymous$$ NS$$anonymous$$:"+TxtAss.text.Length);
             //print(TxtAss.text.ToString());
             JSONObject JsonObj_Received = new JSONObject (content);
             DecryptJsonData(JsonObj_Received);
             
       }
     void DecryptJsonData (JSONObject obj)
         {    
     
             switch (obj.type) {
             case JSONObject.Type.OBJECT:
                 for (int i = 0; i < obj.list.Count; i++) {
                     $$anonymous$$ey = (string)obj.keys [i];
                     JSONObject j = (JSONObject)obj.list [i];
                     //    Debug.Log ("$$anonymous$$ey__" + $$anonymous$$ey);
                     DecryptJsonData (j);
                 }
                 break;
             case JSONObject.Type.ARRAY:
                 foreach (JSONObject j in obj.list) {
                     DecryptJsonData (j);
     
                 }
                 break;
             case JSONObject.Type.STRING:
                 //    
                 if($$anonymous$$ey == "Attributes")
                 {
                  print("Attributes key value :"+ obj.str);
                 }
             break;
     
             }
     
         }

 //   and my text file(in json style) is 
     [{"Attributes":"name1,(,185,300,1,1,1,2000,14904,2383"},
     {"Attributes":"name2,),231,300,1,1,1,2000,18630,2979"},
     {"Attributes":"name3,!,10,1000,1,1,1,4000,5000,800"},
     {"Attributes":"name4,@,13,1000,1,1,1,4000,6250,1000"}]
     

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Joviex · Sep 12, 2012 at 07:26 AM

I think you are fine with the code, the problem is support:

http://docs.unity3d.com/Documentation/Components/class-TextAsset.html

Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:

.txt .html .htm .xml .bytes

Something to ask be added as it is a more common "text" file format now.

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 CarterG81 · Aug 18, 2017 at 04:12 AM 1
Share

Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are: .txt .html .htm .xml .bytes .json .csv .yaml .fnt

avatar image
0

Answer by AaronCammarata · Jul 16, 2013 at 07:17 PM

Agreed - it would be nice if we could modify the list of file extensions associated with each Asset type. I get that it's a slightly more advanced feature, but it would help future-proof the system very nicely.

Just to confirm - there is still no way to load a file with a .json extension as a Text Asset, is there?

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 FiveFingers · Feb 15, 2014 at 12:54 PM

Just save it as .txt

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

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

15 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 avatar image avatar image

Related Questions

Read unicode characters from TextAsset? 0 Answers

Write physically files from resource folder into harddrive 2 Answers

How to write a TextAsset to disk 1 Answer

can use resource.load to load levels(json file)? 1 Answer

How to create a TextAsset in a ScriptedImporter? 2 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