【问题标题】:Cannot access JSON file from Streaming Assets in android build无法从 android build 中的 Streaming Assets 访问 JSON 文件
【发布时间】:2019-07-26 11:50:55
【问题描述】:

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

【问题讨论】:

  • 您遇到的错误是什么?
  • 您可以使用 TextAsset 加载 JSON 文件 (stackoverflow.com/questions/21583104/…)
  • Resources 不是StreamingAssets
  • @Stevy 我在 ADB 上获得了一个空指针引用。
  • @StevenDelrue 我也试过了,仍然得到空指针引用

标签: c# unity3d


【解决方案1】:

我正在使用 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 文件。

【讨论】:

  • 我还没试过这个。让我试试这个然后回复你。
猜你喜欢
  • 2016-04-19
  • 2015-03-09
  • 2012-01-06
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 2018-09-29
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多