ゲーム制作で音ゲーを作っているのですが、
Json形式で保存されたネストされた譜面の情報を値として取り出したいです。
C#
1public class LoadTest : MonoBehaviour 2{ 3 /// <summary> 譜面が保存されている場所 </summary> 4 string filePath = "NotesData/OVERFLOW"; 5 public string FilePath { get; set; } 6 /// <summary> 楽曲名 </summary> 7 public string Name { get; private set; } 8 /// <summary> レーンの総数 </summary> 9 public int MaxBlock { get; private set; } 10 /// <summary> 1分間に四分音符が何拍なるか </summary> 11 public float BPM { get; private set; } 12 /// <summary> 譜面の開始位置 </summary> 13 public int Offset { get; private set; } 14 /// <summary> </summary> 15 public List<NotesData> Notes { get; private set; } 16 /// <summary> 1拍の内にプレイカーソルが何ライン進むか </summary> 17 public List<int> LPB { get; private set; } = new List<int>(); 18 /// <summary> TODO 今のところ何に使うのか分かっていない </summary> 19 public List<int> Num { get; private set; } = new List<int>(); 20 /// <summary> レーンの番号 </summary> 21 public List<int> Block { get; private set; } = new List<int>(); 22 /// <summary> Notesの種類 </summary> 23 public List<int> Type { get; private set; } = new List<int>(); 24 25 public void LoadNotesData() 26 { 27 string input = Resources.Load<TextAsset>(filePath).ToString(); 28 MusicData music = JsonUtility.FromJson<MusicData>(input); 29 StoreNotesData storeNotes = JsonUtility.FromJson<StoreNotesData>(input); 30 31 Name = music.name; 32 MaxBlock = music.maxBlock; 33 BPM = music.BPM; 34 Offset = music.offset; 35 Notes = storeNotes.datas; 36 foreach (var notesInformation in Notes) 37 { 38 LPB.Add(notesInformation.LPB); 39 Num.Add(notesInformation.num); 40 Block.Add(notesInformation.block); 41 Type.Add(notesInformation.type); 42 foreach (var lowerHierarchy in Notes) 43 { 44 LPB.Add(lowerHierarchy.LPB); 45 Num.Add(lowerHierarchy.num); 46 Block.Add(lowerHierarchy.block); 47 Type.Add(lowerHierarchy.type); 48 } 49 } 50 } 51}
C#
1/// <summary> 楽曲の情報を保持する構造体 </summary> 2[Serializable] 3public struct MusicData 4{ 5 /// <summary> 楽曲のタイトル </summary> 6 public string name; 7 /// <summary> レーンの数 </summary> 8 public int maxBlock; 9 /// <summary> 1分間に四分音符を何拍鳴らすか </summary> 10 public float BPM; 11 /// <summary> 譜面の開始位置 </summary> 12 public int offset; 13} 14 15/// <summary> NotesDataを格納する構造体 </summary> 16[SerializeField] 17public struct StoreNotesData 18{ 19 /// <summary> NotesDataのメンバ変数を保持する </summary> 20 public List<NotesData> datas; 21} 22 23/// <summary> Notesの情報を保持する構造体 </summary> 24[Serializable] 25public struct NotesData 26{ 27 /// <summary> 28 /// 1拍の内にプレイカーソルが何ライン進むか 29 /// (LPB値が大きいほどスクロール速度が上がる) 30 /// </summary> 31 public int LPB; 32 /// <summary> TODO </summary> 33 public int num; 34 /// <summary> レーンの番号 </summary> 35 public int block; 36 /// <summary> Notesの種類 </summary> 37 public int type; 38}
JSON
1{ 2 "name": "OVERFLOW", 3 "maxBlock": 5, 4 "BPM": 120, 5 "offset": 0, 6 "notes": [ 7 { 8 "LPB": 4, 9 "num": 4, 10 "block": 0, 11 "type": 1, 12 "notes": [ 13 ] 14 }, 15 { 16 "LPB": 4, 17 "num": 4, 18 "block": 4, 19 "type": 1, 20 "notes": [ 21 ] 22 }, 23 { 24 "LPB": 4, 25 "num": 8, 26 "block": 0, 27 "type": 1, 28 "notes": [ 29 ] 30 }, 31 { 32 "LPB": 4, 33 "num": 8, 34 "block": 4, 35 "type": 1, 36 "notes": [ 37 ] 38 }, 39 { 40 "LPB": 4, 41 "num": 12, 42 "block": 0, 43 "type": 1, 44 "notes": [ 45 ] 46 }, 47 { 48 "LPB": 4, 49 "num": 12, 50 "block": 4, 51 "type": 1, 52 "notes": [ 53 ] 54 }, 55 { 56 "LPB": 4, 57 "num": 16, 58 "block": 2, 59 "type": 1, 60 "notes": [ 61 ] 62 }, 63 { 64 "LPB": 4, 65 "num": 20, 66 "block": 2, 67 "type": 1, 68 "notes": [ 69 ] 70 }, 71 { 72 "LPB": 4, 73 "num": 24, 74 "block": 0, 75 "type": 2, 76 "notes": [ 77 { 78 "LPB": 4, 79 "num": 28, 80 "block": 0, 81 "type": 2, 82 "notes": [ 83 ] 84 } 85 ] 86 }, 87 { 88 "LPB": 4, 89 "num": 24, 90 "block": 4, 91 "type": 2, 92 "notes": [ 93 { 94 "LPB": 4, 95 "num": 28, 96 "block": 4, 97 "type": 2, 98 "notes": [ 99 ] 100 } 101 ] 102 }, 103 { 104 "LPB": 4, 105 "num": 32, 106 "block": 0, 107 "type": 2, 108 "notes": [ 109 { 110 "LPB": 4, 111 "num": 36, 112 "block": 0, 113 "type": 2, 114 "notes": [ 115 ] 116 }, 117 { 118 "LPB": 4, 119 "num": 40, 120 "block": 1, 121 "type": 2, 122 "notes": [ 123 ] 124 }, 125 { 126 "LPB": 4, 127 "num": 44, 128 "block": 1, 129 "type": 2, 130 "notes": [ 131 ] 132 } 133 ] 134 }, 135 { 136 "LPB": 4, 137 "num": 32, 138 "block": 4, 139 "type": 2, 140 "notes": [ 141 { 142 "LPB": 4, 143 "num": 36, 144 "block": 4, 145 "type": 2, 146 "notes": [ 147 ] 148 }, 149 { 150 "LPB": 4, 151 "num": 40, 152 "block": 3, 153 "type": 2, 154 "notes": [ 155 ] 156 }, 157 { 158 "LPB": 4, 159 "num": 44, 160 "block": 3, 161 "type": 2, 162 "notes": [ 163 ] 164 } 165 ] 166 } 167 ] 168}
###やりたいこと
上記のJSONファイルから値を取り出して使いたい。
###やってみたこと
JSONファイルをでシリアライズしてforeach文で値をListに追加した。
###わからなかったこと
JSONファイルの多重ネストの値の取り出し方
【Unity】音ゲーの譜面エディタ「NoteEditor」紹介
を使って譜面作成をしました。
JSONファイルの多重ネストの値の取り出し方が調べても出てこなかったので質問させて戴きました。
わかる方がいましたら回答お願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/20 10:29