前提・実現したいこと
Unityで初めてのゲーム制作で音楽ゲーム(音ゲー)を制作しています。
現在、Jsonファイルを読み込んでオブジェクトを生成するという工程を行っていますがそこで問題が起きています。
発生している問題
読み込みはちゃんとできているはずですがオブジェクトが一つしか生成されません。
該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3using System.IO; 4using System; 5using UnityEngine.UI; 6 7public class GameManager : MonoBehaviour { 8 [Serializable] public class InputJson 9 { 10 public string name; 11 public int BPM; 12 public Note[] Notes; 13} 14[Serializable] public class Note 15 { 16 public int LPB; 17 public int num; 18 public int block; 19 public int type; 20} 21 public GameObject[] notes; 22 private float[] _timing; 23 private int[] _lineNum; 24 public string filePass; 25 private int _notesCount = 0; 26 public float timing; 27 private AudioSource _audioSource; 28 private float _startTime = 0; 29 public float timeOffset = 0; 30 private bool _isPlaying = false; 31 public GameObject startButton; 32 33 void Start(){ 34 _audioSource = GameObject.Find ("GameMusic").GetComponent<AudioSource> (); 35 _timing = new float[1024]; 36 _lineNum = new int[1024]; 37 LoadCSV (); 38 } 39 40 void Update () { 41 if (_isPlaying) { 42 CheckNextNotes (); 43 } 44 45 } 46 47 public void StartGame(){ 48 startButton.SetActive (false); 49 _startTime = Time.time; 50 _audioSource.Play (); 51 _isPlaying = true; 52 } 53 54 void CheckNextNotes(){ 55 while (_timing [_notesCount] + timeOffset < GetMusicTime () && _timing [_notesCount] != 0) { 56 SpawnNotes (_lineNum[_notesCount]); 57 _notesCount++; 58 59 } 60 } 61 62 void SpawnNotes(int num){ 63 Instantiate (notes[num], 64 new Vector3 (-4.0f + (2.0f*num), 10.0f, 0), 65 Quaternion.identity); 66 } 67 68 void LoadCSV(){ 69 string json = Resources.Load (filePass).ToString(); 70 InputJson inputJson = JsonUtility.FromJson<InputJson>(json); 71 Debug.Log(inputJson.name); 72 int i=0; 73 for(int j=0;j < inputJson.Notes.Length; j++){ 74 timing=((float)inputJson.Notes[j].num*4+inputJson.Notes[j].LPB)/100; 75 _timing [i] = timing; 76 _lineNum [i] = inputJson.Notes[j].block; 77 } 78 i++; 79 } 80 float GetMusicTime(){ 81 return Time.time - _startTime; 82 } 83}
Json
1{ 2 "name" : "1F√", 3 "BPM" : "130", 4 "offset" : "4500", 5 "Notes" :[ 6 {"LPB":"3","num":"108","block":"0","type":"1","notes":[]}, 7 {"LPB":"3","num":"108","block":"3","type":"1","notes":[]}, 8 {"LPB":"4","num":"134","block":"1","type":"1","notes":[]}, 9 {"LPB":"4","num":"136","block":"0","type":"1","notes":[]}, 10 {"LPB":"4","num":"138","block":"1","type":"1","notes":[]}, 11 {"LPB":"4","num":"140","block":"2","type":"1","notes":[]}, 12 {"LPB":"4","num":"142","block":"3","type":"1","notes":[]}, 13 {"LPB":"4","num":"146","block":"1","type":"1","notes":[]}, 14 {"LPB":"4","num":"148","block":"0","type":"1","notes":[]}, 15 {"LPB":"4","num":"148","block":"3","type":"1","notes":[]}, 16 {"LPB":"4","num":"150","block":"2","type":"1","notes":[]}, 17 {"LPB":"4","num":"152","block":"0","type":"1","notes":[]}, 18 {"LPB":"4","num":"152","block":"3","type":"1","notes":[]}, 19 {"LPB":"4","num":"154","block":"1","type":"1","notes":[]}, 20 {"LPB":"4","num":"156","block":"0","type":"1","notes":[]}, 21 {"LPB":"4","num":"156","block":"3","type":"1","notes":[]}, 22 {"LPB":"4","num":"158","block":"2","type":"1","notes":[]}, 23 {"LPB":"4","num":"160","block":"3","type":"1","notes":[]}, 24 {"LPB":"4","num":"162","block":"0","type":"1","notes":[]}, 25 {"LPB":"4","num":"164","block":"3","type":"1","notes":[]}, 26 {"LPB":"4","num":"166","block":"1","type":"1","notes":[]}, 27 {"LPB":"4","num":"168","block":"0","type":"1","notes":[]}, 28 {"LPB":"4","num":"170","block":"1","type":"1","notes":[]}, 29 {"LPB":"4","num":"172","block":"2","type":"1","notes":[]}, 30 {"LPB":"4","num":"174","block":"3","type":"1","notes":[]}, 31 {"LPB":"4","num":"176","block":"1","type":"1","notes":[]}, 32 {"LPB":"4","num":"176","block":"2","type":"1","notes":[]}, 33 {"LPB":"4","num":"178","block":"0","type":"1","notes":[]}, 34 {"LPB":"4","num":"180","block":"1","type":"1","notes":[]}, 35 {"LPB":"4","num":"180","block":"2","type":"1","notes":[]}, 36 {"LPB":"4","num":"182","block":"3","type":"1","notes":[]}, 37 {"LPB":"4","num":"184","block":"1","type":"1","notes":[]}, 38 {"LPB":"4","num":"184","block":"2","type":"1","notes":[]}, 39 {"LPB":"4","num":"186","block":"0","type":"1","notes":[]}, 40 {"LPB":"4","num":"188","block":"1","type":"1","notes":[]}, 41 {"LPB":"4","num":"188","block":"2","type":"1","notes":[]}, 42 {"LPB":"4","num":"190","block":"3","type":"1","notes":[]}, 43 {"LPB":"4","num":"192","block":"3","type":"1","notes":[]}, 44 {"LPB":"4","num":"194","block":"0","type":"1","notes":[]}, 45 {"LPB":"4","num":"196","block":"3","type":"1","notes":[]}, 46 {"LPB":"4","num":"198","block":"1","type":"1","notes":[]}, 47 {"LPB":"4","num":"200","block":"0","type":"1","notes":[]}, 48 {"LPB":"4","num":"202","block":"2","type":"1","notes":[]}, 49 {"LPB":"4","num":"704","block":"0","type":"1","notes":[]}, 50 {"LPB":"4","num":"206","block":"2","type":"1","notes":[]}, 51 {"LPB":"4","num":"208","block":"0","type":"1","notes":[]}, 52 {"LPB":"4","num":"208","block":"3","type":"1","notes":[]}, 53 {"LPB":"4","num":"210","block":"2","type":"1","notes":[]}, 54 {"LPB":"4","num":"212","block":"0","type":"1","notes":[]}, 55 {"LPB":"4","num":"212","block":"3","type":"1","notes":[]}, 56 {"LPB":"4","num":"214","block":"1","type":"1","notes":[]}, 57 {"LPB":"4","num":"216","block":"0","type":"1","notes":[]}, 58 {"LPB":"4","num":"216","block":"3","type":"1","notes":[]}, 59 {"LPB":"4","num":"218","block":"2","type":"1","notes":[]}, 60 {"LPB":"4","num":"220","block":"0","type":"1","notes":[]}, 61 {"LPB":"4","num":"220","block":"3","type":"1","notes":[]}, 62 {"LPB":"4","num":"222","block":"1","type":"1","notes":[]}, 63 {"LPB":"4","num":"224","block":"3","type":"1","notes":[]}, 64 {"LPB":"4","num":"226","block":"0","type":"1","notes":[]}, 65 {"LPB":"4","num":"228","block":"3","type":"1","notes":[]}, 66 {"LPB":"4","num":"230","block":"1","type":"1","notes":[]}, 67 {"LPB":"4","num":"232","block":"0","type":"1","notes":[]}, 68 {"LPB":"4","num":"234","block":"1","type":"1","notes":[]}, 69 {"LPB":"4","num":"236","block":"2","type":"1","notes":[]}, 70 {"LPB":"4","num":"238","block":"3","type":"1","notes":[]}, 71 {"LPB":"4","num":"240","block":"1","type":"1","notes":[]}, 72 {"LPB":"4","num":"240","block":"2","type":"1","notes":[]}, 73 {"LPB":"4","num":"242","block":"3","type":"1","notes":[]}, 74 {"LPB":"4","num":"244","block":"1","type":"1","notes":[]}, 75 {"LPB":"4","num":"244","block":"2","type":"1","notes":[]}, 76 {"LPB":"4","num":"246","block":"0","type":"1","notes":[]}, 77 {"LPB":"4","num":"248","block":"1","type":"1","notes":[]}, 78 {"LPB":"4","num":"248","block":"2","type":"1","notes":[]}, 79 {"LPB":"4","num":"250","block":"3","type":"1","notes":[]}, 80 {"LPB":"4","num":"252","block":"1","type":"1","notes":[]}, 81 {"LPB":"4","num":"252","block":"2","type":"1","notes":[]}, 82 {"LPB":"4","num":"254","block":"0","type":"1","notes":[]}, 83 {"LPB":"4","num":"256","block":"3","type":"1","notes":[]}, 84 {"LPB":"4","num":"258","block":"0","type":"1","notes":[]}, 85 {"LPB":"4","num":"260","block":"3","type":"1","notes":[]}, 86 {"LPB":"4","num":"262","block":"2","type":"1","notes":[]}, 87 {"LPB":"4","num":"264","block":"0","type":"1","notes":[]}, 88 {"LPB":"4","num":"266","block":"2","type":"1","notes":[]} 89 ]} 90
試したこと
参考にしている記事
[Unity]シンプルな音ゲーの作り方 part2 ゲーム製作
UnityでJSONファイルを読み込むメモ
そのほかにもネットで調べたり、自分が持っている参考書を見ました。
補足情報(FW/ツールのバージョンなど)
Unity 2020.1.5f1
NoteEditor
【Unity】音ゲーの譜面エディタ「NoteEditor」紹介
回答1件
あなたの回答
tips
プレビュー