- Home /
Cannot access JSON file from Streaming Assets in android build
I'm relatively new to Unity. I ran into the following problem when I built it for android. The JSON file doesn't load on build. However, it loads in editor. I've tried multiple techniques, like Streaming Assets folder, Resources, persistentDataPath. None of them seems to be working for builds.
public class setting_data
{
private Eng_WB_Question[] gamedata;
private string filename = "words.json";
public Eng_WB_Controller controller;
string json_data;
private void Start()
{
// LoadJSON();// this isn't necessary because I am calling the LoadJSON function from another class.
}
public void LoadJSON()
{
Debug.Log(json_data);
json_data = Resources.Load(filename).ToString();
Eng_WB_Question_Wrapper loaded_data = JsonUtility.FromJson<Eng_WB_Question_Wrapper>(json_data);
gamedata = loaded_data.questions;
controller.gamedata_loaded(gamedata);
}
}
try using ins$$anonymous$$d of your resource load
var jsonTextFile = Resources.Load<TextAsset>(filename");
Answer by Bunny83 · Jul 30, 2019 at 01:19 AM
Your question is very confusing. Your title talks about the streaming assets folder which is completely seperate from Unity's resources system. On Android in particular the streaming assets are not located in an accessible folder since all files that come with an app are located inside the APK file. You can only access streaming assets through the WWW class or the UnityWebRequest class. Otherwise you would need third party software to access files inside a zip archive (since that's what an APK file is).
Since the code you have included in your question does not use the streaming assets but Unity's resources folder it's a completely different story. Resources.Load works with assetnames, not file names. The file extension must be omitted when calling Resources.Load. Also keep in mind that the asset path has to be relative to the Resources folder and if sub folders are used you have to use forward slashes. So if you have a file named "words.json" inside a resources folder, you will load the text asset through
TextAsset jsonTextAsset = Resources.Load<TextAsset>("words");
Next thing is when loading an asset through Resources.Load you actually get an asset. In the case of .txt or .json files this is a TextAsset. You have to use TextAsset.text and not ToString.
If you actually want to use Streaming Assets you should read the documentation carefully:
Android uses files inside a compressed APK/JAR file, "jar:file://" + Application.dataPath + "!/assets".
To read streaming Assets on platforms like Android and WebGL , where you cannot access streaming Asset files directly, use UnityWebRequest. For an example, see Application.streamingAssetsPath.
Your answer
Follow this Question
Related Questions
Streaming Assets android 0 Answers
File.WriteAllText() on Android (Json file) 2 Answers
Stuck while building for Android - Compiling Resources | Repackages Resources 1 Answer
Application.dataPath is incorrect after install on Android 0 Answers
how to integrate 2 unity activity within 1 android app 0 Answers