[英]Loading Text File From Resource
我一直在嘗試從.JSON文件中檢索一些數據。 我目前將Unity與SimpleJson一起使用,當我加載游戲時,出現此錯誤:
NullReferenceException:對象引用未設置為對象的實例PlayerInteraction.test()(在Assets / Scripts / PlayerInteraction.cs:93上)PlayerInteraction.Start()(在Assets / Scripts / PlayerInteraction.cs:23上)
這是代碼:
void test(){
string filepath = (Application.dataPath + "/listofgames.txt");
TextAsset file = Resources.Load(filepath) as TextAsset;
var node = JSON.Parse(file.text); //line 93
var pl = node["apps"];
Debug.Log("TEST: " + pl[0]);
}
Resources.load
僅適用於Resources文件夾中的文件。 嘗試將文件listofgames.txt
移動到Resources文件夾(如果需要,請創建它),然后調用Resources.load("listofgames")
(必須省略文件擴展名)。
請注意,您還可以具有TextAsset
字段,以便可以在檢查器中引用它。 這適用於TextAsset
之外的TextAsset
:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public TextAsset asset;
void Start() {
print(asset.text);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.