前提・実現したいこと
現在UnityでAndroid向けの音楽ゲームを制作中です。任意のフォルダ内の譜面ファイル(json形式)をWWWを用いて読み込み、String型の配列に格納する所まではできたのですが、格納したテキストから楽曲情報を取得することができませんでした。情報の取得にはJsonNodeを使用しています。解決策等ありましたらよろしくお願いいたします。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object JsonNode.get_Item (System.String key) (at Assets/Plugins/JsonNode/JsonNode.cs:72) MusicData.SetParameter (System.String jsontext, System.Int32 indexNum) (at Assets/Scripts/Scenes/MusicSelect/MusicData.cs:16) MusicSelect.LoadMusicData () (at Assets/Scripts/Scenes/MusicSelect/MusicSelect.cs:34) MusicSelect.Start () (at Assets/Scripts/Scenes/MusicSelect/MusicSelect.cs:24)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using MiniJSON; 5 6public class MusicData : MonoBehaviour 7{ 8 string title; 9 int bpm; 10 int index; 11 Sprite thumbnail; 12 13 public void SetParameter(string jsontext, int indexNum) 14 { 15 JsonNode json = JsonNode.Parse(jsontext); 16 title = json["name"].Get<string>(); 17 bpm = (int)json["BPM"].Get<long>(); 18 index = indexNum; 19 } 20 21 public string GetTitle() 22 { 23 return title; 24 } 25 26 public int GetBPM() 27 { 28 return bpm; 29 } 30}
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using MiniJSON; 5 6public class JsonManager : MonoBehaviour 7{ 8 static public JsonManager jsonManager; 9 10 static public string[] jsonTexts; 11 12 private void Awake() 13 { 14 if (jsonManager == null) 15 { 16 jsonManager = this; 17 DontDestroyOnLoad(gameObject); 18 } 19 else 20 { 21 Destroy(gameObject); 22 } 23 } 24 25 private void Start() 26 { 27 LoadJson(); 28 } 29 30 void LoadJson() 31 { 32 jsonTexts = new string[GameManager.MusicPath.Length]; 33 34 for (int i = 0; i < jsonTexts.Length; i++) 35 { 36 WWW www = new WWW("file:///" + GameManager.MusicPath[i].Replace("\", "/") + "/Notes.json"); 37 Debug.Log(www.text); 38 jsonTexts[i] = www.text; 39 } 40 } 41}
試したこと
MiniJSONで取得する方法も試してみましたがうまくいきませんでした。
【2020/2/28 追記】
Resources内のjsonファイルの場合はJsonNodeを用いて情報を取得することができます。Resourcesを使う場合とWWWを使う場合で何か異なる処理が必要になるのでしょうか?
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class SampleClass : MonoBehaviour 6{ 7 void Start() 8 { 9 //Resourcesフォルダ内のjsonを読み込む場合 10 LoadSample1(); 11 //WWWクラスで任意のフォルダ内のjsonを読み込む場合 12 LoadSample2(); 13 } 14 15 void LoadSample1() 16 { 17 Debug.Log("Resourcesフォルダ内のjsonを読み込む場合"); 18 19 string jsontext = Resources.Load<TextAsset>("Sample/Notes").ToString(); 20 Debug.Log(jsontext); 21 22 JsonNode json = JsonNode.Parse(jsontext); 23 Debug.Log(json); 24 25 string title = json["name"].Get<string>(); 26 Debug.Log("タイトル:" + title); 27 int bpm = (int)json["BPM"].Get<long>(); 28 Debug.Log("BPM:" + bpm); 29 } 30 31 void LoadSample2() 32 { 33 Debug.Log("WWWクラスで任意のフォルダ内のjsonを読み込む場合"); 34 35 WWW www = new WWW("file:///" + GameManager.MusicPath[0].Replace("\", "/") + "/Notes.json"); 36 string jsontext = www.text; 37 Debug.Log(jsontext); 38 39 JsonNode json = JsonNode.Parse(jsontext); 40 Debug.Log(json); 41 42 string title = json["name"].Get<string>(); 43 Debug.Log("タイトル:" + title); 44 int bpm = (int)json["BPM"].Get<long>(); 45 Debug.Log("BPM:" + bpm); 46 } 47}
補足情報(FW/ツールのバージョンなど)
Unity 2019.2.18f1

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/28 10:42
2020/02/28 11:50
2020/02/28 13:04
2020/02/28 15:49 編集
2020/02/28 16:23