60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using System.IO;
|
|
using UnityEngine;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public BeatmapManager beatmapManager; // 负责加载和管理谱面
|
|
public NoteSpawner noteSpawner; // 音符生成器
|
|
public AudioSource musicSource; // 音乐播放器
|
|
|
|
void Start()
|
|
{
|
|
Debug.Log("GameManager Start() 被调用");
|
|
|
|
// 检查关键组件是否为空
|
|
if (beatmapManager == null) Debug.LogError("beatmapManager 未赋值!");
|
|
if (noteSpawner == null) Debug.LogError("noteSpawner 未赋值!");
|
|
if (musicSource == null) Debug.LogError("musicSource 未赋值!");
|
|
|
|
// 在游戏开始时加载并初始化谱面
|
|
string beatmapFilePath = Path.Combine(Application.streamingAssetsPath, "Emilia_demo.json");
|
|
|
|
if (!File.Exists(beatmapFilePath))
|
|
{
|
|
Debug.LogError($"谱面文件不存在: {beatmapFilePath}");
|
|
return;
|
|
}
|
|
|
|
string json = File.ReadAllText(beatmapFilePath);
|
|
Beatmap beatmap = JsonUtility.FromJson<Beatmap>(json); // 解析 JSON 为 Beatmap 对象
|
|
|
|
if (beatmap == null)
|
|
{
|
|
Debug.LogError("谱面解析失败,JSON 格式可能错误!");
|
|
return;
|
|
}
|
|
|
|
beatmapManager.LoadBeatmap(beatmap); // 传递给 BeatmapManager
|
|
noteSpawner.LoadBeatmap(beatmap); // 传递给 NoteSpawner
|
|
|
|
// 播放背景音乐
|
|
if (!string.IsNullOrEmpty(beatmap.musicFile))
|
|
{
|
|
AudioClip musicClip = Resources.Load<AudioClip>(beatmap.musicFile);
|
|
if (musicClip == null)
|
|
{
|
|
Debug.LogError($"音乐文件加载失败: {beatmap.musicFile}");
|
|
}
|
|
else
|
|
{
|
|
musicSource.clip = musicClip;
|
|
musicSource.Play();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("谱面中 musicFile 为空!");
|
|
}
|
|
}
|
|
}
|