【问题标题】:About Android can't load json file in Application.persistentDataPath关于Android can't load json file in Application.persistentDataPath
【发布时间】:2019-08-16 07:54:35
【问题描述】:

我把 json 文件放在 Application.persistentDataPath 中,然后它就可以在 PC 上加载和工作了。

但是在Android上不行,卡了好久。

我找到了很多方法,但没有人能解决它。

请帮帮我,谢谢!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;




public class QuizCon : MonoBehaviour
{
 public RoundData[] allRoundData;
 private string gameDataFileName = "quiz.json";


 private void Start()
 {

     DontDestroyOnLoad(gameObject);
     LoadGameData();

 }

public RoundData GetCurrectRoundData()
 {
     return allRoundData[0];
 }

private void LoadGameData()
 {
     string filePath = Path.Combine(Application.persistentDataPath, gameDataFileName);
     if(File.Exists(filePath))
     {
         string dataJson = File.ReadAllText(filePath);
         GameData loadedData = JsonUtility.FromJson<GameData>(dataJson);

         allRoundData = loadedData.allRoundData;
     }
     else
     {
         Debug.LogError("Cannot load game data!");
     }
 }

【问题讨论】:

    标签: android json unity3d


    【解决方案1】:

    尝试将文件写入外部 (SDCard) 而不是内部。

    您可以在 PlayerSettings -> Android 下更改此设置

    【讨论】:

    • 我解决了这个问题。我将文件放在 StreamingAssets 中,并使用 UnityWebRequest 获取文件。谢谢!
    猜你喜欢
    • 2018-01-11
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2016-10-16
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多