Ahmed Arsalan
Ahmed Arsalan

Reputation: 1

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

Upvotes: 0

Views: 1085

Answers (1)

Anjuman noor
Anjuman noor

Reputation: 74

I am using UnityWebRequest to get the json file from the streaming asset folder. my code is:

void Awake()
    {

        filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "CountryCodes2.json");

             StartCoroutine(Example());  
    }

    IEnumerator Example() 
    {
        if (filePath.Contains("://")) 
        {
            UnityWebRequest www = new UnityWebRequest(filePath);
            yield return www.SendWebRequest();
            result = www.downloadHandler.text;
        }
        else
            result = System.IO.File.ReadAllText(filePath);

     }

you can read your json file like this.

Upvotes: 1

Related Questions