add the connections and sendToTest.cs for connecting with the game test
This commit is contained in:
@@ -1,11 +1,58 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public BeatmapManager beatmapManager; // 负责加载和管理谱面
|
||||
public NoteSpawner noteSpawner; // 音符生成器
|
||||
public AudioSource musicSource; // 音乐播放器
|
||||
[Header("paths")]
|
||||
// 变量声明不再从静态变量中读取
|
||||
public string JSONPath;
|
||||
public string audioPath;
|
||||
public string bgPicPath;
|
||||
|
||||
// Optional: sprite renderer to show background image
|
||||
public SpriteRenderer backgroundRenderer;
|
||||
|
||||
// NEW: UI Text to display banflag content from pressStart
|
||||
[Header("Debug / Test")]
|
||||
public Text banflagTextUI;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// 订阅暂停管理器的事件
|
||||
if (PauseManager.Instance != null)
|
||||
{
|
||||
PauseManager.Instance.OnPauseStateChanged += HandlePauseStateChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// 取消订阅暂停管理器的事件
|
||||
if (PauseManager.Instance != null)
|
||||
{
|
||||
PauseManager.Instance.OnPauseStateChanged -= HandlePauseStateChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePauseStateChanged(bool isPaused)
|
||||
{
|
||||
if (musicSource != null)
|
||||
{
|
||||
if (isPaused)
|
||||
{
|
||||
musicSource.Pause();
|
||||
}
|
||||
else
|
||||
{
|
||||
musicSource.UnPause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -16,6 +63,34 @@ public class GameManager : MonoBehaviour
|
||||
if (noteSpawner == null) Debug.LogError("noteSpawner 未赋值!");
|
||||
if (musicSource == null) Debug.LogError("musicSource 未赋值!");
|
||||
|
||||
// 如果 pressStart 已经读取了 banflag 内容,则把它显示在 UI(如果分配了)
|
||||
if (banflagTextUI != null && !string.IsNullOrEmpty(pressStart.banflagContent))
|
||||
{
|
||||
banflagTextUI.text = pressStart.banflagContent;
|
||||
Debug.Log("Displayed banflag content from pressStart into banflagTextUI.");
|
||||
}
|
||||
|
||||
// 在 test mode 下使用外部路径预加载资源并暂停系统,等待按空格继续
|
||||
if (GameConfig.testMode)
|
||||
{
|
||||
// 延迟赋值:在 Start() 中获取静态路径,此时 pressStart.cs 已经运行并更新了它们
|
||||
JSONPath = pressStart.testmode_beatmapJSON_path;
|
||||
audioPath = pressStart.testmode_audioFile_path;
|
||||
bgPicPath = pressStart.testmode_bgPicFile_path;
|
||||
|
||||
Debug.Log("Test mode active: using external paths to preload assets");
|
||||
|
||||
// also update banflag UI if content exists
|
||||
if (banflagTextUI != null && !string.IsNullOrEmpty(pressStart.banflagContent))
|
||||
{
|
||||
banflagTextUI.text = pressStart.banflagContent;
|
||||
}
|
||||
|
||||
StartCoroutine(HandleTestModeStartup());
|
||||
return;
|
||||
}
|
||||
|
||||
// 以下为原有流程(已保留注释,未改写)
|
||||
// 在游戏开始时加载并初始化谱面
|
||||
string beatmapFilePath = Path.Combine(Application.streamingAssetsPath, "Emilia_demo.json");
|
||||
|
||||
@@ -26,6 +101,7 @@ public class GameManager : MonoBehaviour
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(beatmapFilePath);
|
||||
// 假设 Beatmap 是一个已定义的类
|
||||
Beatmap beatmap = JsonUtility.FromJson<Beatmap>(json); // 解析 JSON 为 Beatmap 对象
|
||||
|
||||
if (beatmap == null)
|
||||
@@ -37,7 +113,7 @@ public class GameManager : MonoBehaviour
|
||||
beatmapManager.LoadBeatmap(beatmap); // 传递给 BeatmapManager
|
||||
noteSpawner.LoadBeatmap(beatmap); // 传递给 NoteSpawner
|
||||
|
||||
// 播放背景音乐
|
||||
// 播放背景音乐(延迟播放由 beatmapManager.globalDelaySeconds 决定)
|
||||
if (!string.IsNullOrEmpty(beatmap.musicFile))
|
||||
{
|
||||
AudioClip musicClip = Resources.Load<AudioClip>(beatmap.musicFile);
|
||||
@@ -48,7 +124,16 @@ public class GameManager : MonoBehaviour
|
||||
else
|
||||
{
|
||||
musicSource.clip = musicClip;
|
||||
musicSource.Play();
|
||||
float delay = beatmapManager != null ? beatmapManager.globalDelaySeconds : 0f;
|
||||
if (delay > 0f)
|
||||
{
|
||||
musicSource.PlayDelayed(delay);
|
||||
Debug.Log($"Scheduled music to play after {delay} seconds.");
|
||||
}
|
||||
else
|
||||
{
|
||||
musicSource.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -56,4 +141,195 @@ public class GameManager : MonoBehaviour
|
||||
Debug.LogError("谱面中 musicFile 为空!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator HandleTestModeStartup()
|
||||
{
|
||||
// Preload audio from audioPath (absolute)
|
||||
if (!string.IsNullOrEmpty(audioPath))
|
||||
{
|
||||
if (!File.Exists(audioPath))
|
||||
{
|
||||
Debug.LogError($"TestMode audio file not found: {audioPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 注意:这里仍然使用过时的 WWW,建议替换为 UnityWebRequest
|
||||
string url = "file://" + audioPath;
|
||||
using (var www = new WWW(url))
|
||||
{
|
||||
yield return www;
|
||||
if (!string.IsNullOrEmpty(www.error))
|
||||
{
|
||||
Debug.LogError($"加载测试模式音频失败: {www.error}");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
AudioClip clip = www.GetAudioClip(false, false);
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogError("从文件获取 AudioClip 失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
musicSource.clip = clip;
|
||||
musicSource.loop = false;
|
||||
Debug.Log("TestMode audio loaded into AudioSource");
|
||||
}
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"将音频设置到 AudioSource 时出错: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TestMode audioPath 未配置。");
|
||||
}
|
||||
|
||||
// Preload background image into sprite renderer
|
||||
if (backgroundRenderer != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(bgPicPath))
|
||||
{
|
||||
if (!File.Exists(bgPicPath))
|
||||
{
|
||||
Debug.LogError($"TestMode background image not found: {bgPicPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] imgBytes = null;
|
||||
try
|
||||
{
|
||||
imgBytes = File.ReadAllBytes(bgPicPath);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"读取背景图片文件出错: {ex}");
|
||||
}
|
||||
|
||||
if (imgBytes != null)
|
||||
{
|
||||
Texture2D tex = new Texture2D(2, 2);
|
||||
if (tex.LoadImage(imgBytes))
|
||||
{
|
||||
Sprite s = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||
backgroundRenderer.sprite = s;
|
||||
|
||||
// 【修改点:设置 SpriteRenderer 透明度为 255 (1.0f)】
|
||||
Color color = backgroundRenderer.color;
|
||||
color.a = 1.0f;
|
||||
backgroundRenderer.color = color;
|
||||
|
||||
// 保持比例不变的最大适应由场景布局决定,记录 assignment
|
||||
Debug.Log("Background sprite assigned (ensure your SpriteRenderer size fits scene)");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"无法从字节创建 Texture2D: {bgPicPath}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TestMode bgPicPath 未配置。");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("backgroundRenderer 未配置,跳过背景加载。");
|
||||
}
|
||||
|
||||
// Read JSON from JSONPath and parse via BeatmapManager.ParseJsonOnly
|
||||
if (!string.IsNullOrEmpty(JSONPath))
|
||||
{
|
||||
if (!File.Exists(JSONPath))
|
||||
{
|
||||
Debug.LogError($"TestMode beatmap JSON not found: {JSONPath}");
|
||||
yield break; // abort
|
||||
}
|
||||
|
||||
string jsonContent = null;
|
||||
try
|
||||
{
|
||||
jsonContent = File.ReadAllText(JSONPath);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"读取 TestMode JSON 文件出错: {ex}");
|
||||
yield break;
|
||||
}
|
||||
|
||||
bool parsed = beatmapManager.ParseJsonOnly(jsonContent);
|
||||
if (!parsed)
|
||||
{
|
||||
Debug.LogError("ParseJsonOnly 失败,停止启动测试模式。");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Do not start spawning yet. We will start after unpausing.
|
||||
Debug.Log("Beatmap parsed (deferred start). Ready to unpause.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("TestMode JSONPath 未配置。");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Pause the system until Space pressed
|
||||
// Use PauseManager instead of directly setting Time.timeScale
|
||||
PauseManager.Instance?.Pause(true);
|
||||
UpdateStatusOnConsole("Paused: press Space to start playback");
|
||||
|
||||
// Wait for space key down (use unscaled input loop)
|
||||
while (!Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Resume using PauseManager
|
||||
PauseManager.Instance?.Pause(false);
|
||||
|
||||
// Start spawning using the parsed beatmap
|
||||
if (beatmapManager.beatmap != null)
|
||||
{
|
||||
beatmapManager.LoadBeatmap(beatmapManager.beatmap);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("启动播放失败:beatmap 未解析或为空");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// If audio clip was loaded into musicSource.clip, play now with delay from parsed globalDelaySeconds
|
||||
float delaySeconds = beatmapManager != null ? beatmapManager.globalDelaySeconds : 0f;
|
||||
if (musicSource.clip != null)
|
||||
{
|
||||
if (delaySeconds > 0f)
|
||||
{
|
||||
musicSource.PlayDelayed(delaySeconds);
|
||||
Debug.Log($"Scheduled test-mode music to play after {delaySeconds} seconds.");
|
||||
}
|
||||
else
|
||||
{
|
||||
musicSource.Play();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TestMode: no audio clip loaded into AudioSource; skipping play.");
|
||||
}
|
||||
|
||||
UpdateStatusOnConsole("Playback started");
|
||||
}
|
||||
|
||||
private void UpdateStatusOnConsole(string message)
|
||||
{
|
||||
Debug.Log(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user