当我在Android上构建时,我的json文件似乎无法加载,因为我没有看到数据。在统一编辑器播放它工作正常。我正在努力解决在PC上工作但在Android上工作的问题。我想这只是路径差异,但初学者并不容易。
这是我的代码TRY 1:
public static JsonData LoadFile(string path)
{
var fileContents = File.ReadAllText(path);
var data = JsonMapper.ToObject(fileContents);
return data;
}
public void QuestionLoader()
{
// Load the Json file and store its contents in a 'JsonData' variable
var data = LoadFile(Application.dataPath + "/Resources/json/level1.json");
以下是我的尝试2 - 基于Unity手册 - https://docs.unity3d.com/Manual/StreamingAssets.html:
public static JsonData LoadFile(string path)
{
var www = new WWW(path);
return www.text;
//var fileContents = File.ReadAllText(path);
// var data = JsonMapper.ToObject(fileContents);
// return data;
}
public void QuestionLoader()
{
string path = "";
// Load the Json file and store its contents
#if UNITY_ANDROID
path = "jar:file://" + Application.dataPath + "!/assets/Levels/level1.json";
#endif
#if UNITY_EDITOR
path = Application.dataPath + "/StreamingAssets/Levels/level1.json";
#endif
//
var data = LoadFile(path);