- Home /
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?
how to load the json/text file in android?application.datapath is not working!!any help?
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.
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"}]
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.
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
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?