SoundGameController.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections;
  2. using UnityEngine;
  3. public class SoundGameController : MonoBehaviour
  4. {
  5. SoundTrackManager soundTrackManager = new();
  6. GameObject notePrefabSwipe, notePrefabTap;
  7. // Start is called once before the first execution of Update after the MonoBehaviour is created
  8. void Start()
  9. {
  10. TextAsset soundTrackJson = Resources.Load<TextAsset>("WalkDogs/SoundTrack/track_1");
  11. if (soundTrackJson != null)
  12. {
  13. string soundTrackString = soundTrackJson.text;
  14. soundTrackManager.ImportFromJson(soundTrackString);
  15. }
  16. notePrefabSwipe = Resources.Load<GameObject>("WalkDogs/Note/NoteSwipe");
  17. notePrefabTap = Resources.Load<GameObject>("WalkDogs/Note/NoteTap");
  18. StartCoroutine(PlaySoundGame());
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. }
  24. IEnumerator PlaySoundGame()
  25. {
  26. float startTime = Time.time;
  27. float endTime = startTime + soundTrackManager.length;
  28. while (Time.time < endTime)
  29. {
  30. foreach (var note in soundTrackManager.soundAction)
  31. {
  32. if (!note.isActive && Time.time >= startTime - GameData.noteStartLeadTime)
  33. {
  34. note.isActive = true;
  35. CreateNote(note.time, note.action);
  36. continue;
  37. }
  38. }
  39. }
  40. return null;
  41. }
  42. private void CreateNote(float hitTime, string action)
  43. {
  44. GameObject note;
  45. if (action == "tap")
  46. {
  47. note = Instantiate(notePrefabTap);
  48. }
  49. else
  50. {
  51. note = Instantiate(notePrefabSwipe);
  52. }
  53. NoteController noteController = note.GetComponent<NoteController>();
  54. noteController.speed = soundTrackManager.speed;
  55. noteController.hitTime = hitTime;
  56. noteController.action = action;
  57. }
  58. }