Unity打包安卓平台读取StreamingAssets文件夹下的配置文件

 

  

    /// <summary>
    /// 从SrreamingAssets获取本地 JSON 文件内容,如果不存在则下载并保存到持久化数据路径
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string GetLocalJson(string path) 
    {
        string dbPath = string.Empty;
        if (Application.platform == RuntimePlatform.Android)
        {
            dbPath = Path.Combine(Application.persistentDataPath, path);
        }
        else
        {
            dbPath = Path.Combine(Application.streamingAssetsPath, path);
        }
        if (!File.Exists(dbPath))
        {
            string sourcePath = "jar:file://" + Application.dataPath + "!/assets/" + path;
            using (UnityWebRequest www = UnityWebRequest.Get(sourcePath))
            {
                www.SendWebRequest();

                while (!www.isDone)
                {
                    // 可以在这里更新加载 UI
                }

                if (www.result == UnityWebRequest.Result.Success)
                {
View Code

 


通过上述代码可以将原本streamingAssets路径下的文件复制到安卓硬件的存储空间中,解决了无法读取StreamingAssets的问题

posted @ 2025-04-10 16:46  SummerTrainnn  阅读(180)  评论(0)    收藏  举报