質問編集履歴

1

Resourcesフォルダを使う場合とWWWを使う場合の比較を追記しました

2020/02/28 10:34

投稿

Yu086
Yu086

スコア5

test CHANGED
File without changes
test CHANGED
@@ -194,6 +194,116 @@
194
194
 
195
195
 
196
196
 
197
+ 【2020/2/28 追記】
198
+
199
+ Resources内のjsonファイルの場合はJsonNodeを用いて情報を取得することができます。Resourcesを使う場合とWWWを使う場合で何か異なる処理が必要になるのでしょうか?
200
+
201
+
202
+
203
+ ```C#
204
+
205
+ using System.Collections;
206
+
207
+ using System.Collections.Generic;
208
+
209
+ using UnityEngine;
210
+
211
+
212
+
213
+ public class SampleClass : MonoBehaviour
214
+
215
+ {
216
+
217
+ void Start()
218
+
219
+ {
220
+
221
+ //Resourcesフォルダ内のjsonを読み込む場合
222
+
223
+ LoadSample1();
224
+
225
+ //WWWクラスで任意のフォルダ内のjsonを読み込む場合
226
+
227
+ LoadSample2();
228
+
229
+ }
230
+
231
+
232
+
233
+ void LoadSample1()
234
+
235
+ {
236
+
237
+ Debug.Log("Resourcesフォルダ内のjsonを読み込む場合");
238
+
239
+
240
+
241
+ string jsontext = Resources.Load<TextAsset>("Sample/Notes").ToString();
242
+
243
+ Debug.Log(jsontext);
244
+
245
+
246
+
247
+ JsonNode json = JsonNode.Parse(jsontext);
248
+
249
+ Debug.Log(json);
250
+
251
+
252
+
253
+ string title = json["name"].Get<string>();
254
+
255
+ Debug.Log("タイトル:" + title);
256
+
257
+ int bpm = (int)json["BPM"].Get<long>();
258
+
259
+ Debug.Log("BPM:" + bpm);
260
+
261
+ }
262
+
263
+
264
+
265
+ void LoadSample2()
266
+
267
+ {
268
+
269
+ Debug.Log("WWWクラスで任意のフォルダ内のjsonを読み込む場合");
270
+
271
+
272
+
273
+ WWW www = new WWW("file:///" + GameManager.MusicPath[0].Replace("\", "/") + "/Notes.json");
274
+
275
+ string jsontext = www.text;
276
+
277
+ Debug.Log(jsontext);
278
+
279
+
280
+
281
+ JsonNode json = JsonNode.Parse(jsontext);
282
+
283
+ Debug.Log(json);
284
+
285
+
286
+
287
+ string title = json["name"].Get<string>();
288
+
289
+ Debug.Log("タイトル:" + title);
290
+
291
+ int bpm = (int)json["BPM"].Get<long>();
292
+
293
+ Debug.Log("BPM:" + bpm);
294
+
295
+ }
296
+
297
+ }
298
+
299
+ ```
300
+
301
+
302
+
303
+ ![実行画面](e0c21e27ce965c0d35d201e702b3e7e9.png)
304
+
305
+
306
+
197
307
  ### 補足情報(FW/ツールのバージョンなど)
198
308
 
199
309