因此,对于我的游戏,我希望能够通过读取JSON文件轻松添加新的射弹,而这对我来说不起作用。我以前从未真正使用过JSON,所以我是JSON的新手,只是让自己感到困惑。这是我正在使用的JSON的代码和示例(N.B.我正在使用C#):
public class JSONTest : MonoBehaviour {
{
[Serializable]
public class Root
{
public Projectile[] proj;
}
[Serializable]
public class Projectile {
public int ID;
public string name;
public float velocity;
public int bounces;
public bool explosive;
}
public static Root JsonConvert(string Json)
{
return JsonUtility.FromJson<Root>(Json);
}
}
// Start is called before the first frame update
void Start()
{
string jsonString = File.ReadAllText(Application.dataPath + "/JSON/Projectiles.json"); //reads the file into string
Debug.Log(jsonString);
Root test = Projectile.JsonConvert(jsonString);
Debug.Log(test.proj.Length);
}
}
JSON:
{
"Projectiles":[{
"ID":1,
"name":"Bullet",
"velocity":1.5,
"bounces":3,
"explosive":false
}, {
"ID":2,
"name":"Rocket",
"velocity":3,
"bounces":1,
"explosive":true
}]
}
每次运行时,我在第二个Debug.Log()上都会收到NullReferenceException。
任何帮助将不胜感激