首页 > 解决方案 > 无法从 android build 中的 Streaming Assets 访问 JSON 文件

问题描述

我对 Unity 比较陌生。当我为android构建它时遇到了以下问题。JSON 文件不会在构建时加载。但是,它会在编辑器中加载。

我尝试了多种技术,例如 Streaming Assets 文件夹、Resources、persistentDataPath。他们似乎都没有为构建工作。

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

标签: c#unity3d

解决方案


我正在使用 UnityWebRequest 从流资产文件夹中获取 json 文件。我的代码是:

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

     }

你可以像这样读取你的 json 文件。


推荐阅读