2025-02-14 22:04:21 -05:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using Ichni.RhythmGame;
|
|
|
|
|
|
using UnityEngine;
|
2025-06-21 23:09:34 +08:00
|
|
|
|
using UnityEngine.InputSystem;
|
2025-02-14 22:04:21 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Ichni.Editor
|
|
|
|
|
|
{
|
2026-03-14 02:30:26 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 编辑器的音乐播放控制器。
|
|
|
|
|
|
/// 实现 ISongTimeProvider,作为编辑器的标准时间来源,
|
|
|
|
|
|
/// 在 EditorManager.Awake 中注册到 CoreServices.TimeProvider。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MusicPlayer : MonoBehaviour, ISongTimeProvider
|
2025-02-14 22:04:21 -05:00
|
|
|
|
{
|
2026-03-14 02:30:26 -04:00
|
|
|
|
#region [ISongTimeProvider 实现] ISongTimeProvider Implementation
|
|
|
|
|
|
/// <summary>当前播放进度(秒),已扣除 offset,供 CoreServices.TimeProvider 使用</summary>
|
|
|
|
|
|
public float SongTime => EditorManager.instance.songInformation.songTime;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>当前是否正在播放</summary>
|
|
|
|
|
|
public bool IsPlaying => isPlaying;
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region [运行时缓存数据] Property Caches
|
2025-05-04 03:29:23 -04:00
|
|
|
|
public bool isDebugging;
|
2025-02-14 22:04:21 -05:00
|
|
|
|
public bool isPlaying;
|
|
|
|
|
|
public AudioSource audioSource;
|
2025-07-12 18:56:10 +08:00
|
|
|
|
private float DspTime => (float)AudioSettings.dspTime;
|
2026-03-14 02:30:26 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region [生命周期] Lifecycle
|
2025-02-26 00:52:08 -05:00
|
|
|
|
private void Update()
|
2025-02-14 22:04:21 -05:00
|
|
|
|
{
|
2025-05-04 03:29:23 -04:00
|
|
|
|
if (isDebugging)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorManager.instance.songInformation.songTime += Time.deltaTime;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-05-30 19:37:54 +08:00
|
|
|
|
|
2025-02-26 00:52:08 -05:00
|
|
|
|
if (isPlaying)
|
|
|
|
|
|
{
|
2026-03-14 02:30:26 -04:00
|
|
|
|
EditorManager.instance.songInformation.songTime =
|
|
|
|
|
|
audioSource.time - EditorManager.instance.songInformation.offset;
|
2025-02-26 00:52:08 -05:00
|
|
|
|
}
|
2025-02-14 22:04:21 -05:00
|
|
|
|
}
|
2026-03-14 02:30:26 -04:00
|
|
|
|
#endregion
|
2025-02-14 22:04:21 -05:00
|
|
|
|
|
2026-03-14 02:30:26 -04:00
|
|
|
|
#region [播放控制] Playback Control
|
2025-02-14 22:04:21 -05:00
|
|
|
|
public void PlayMusic()
|
|
|
|
|
|
{
|
2025-02-20 03:15:42 +08:00
|
|
|
|
isPlaying = !isPlaying;
|
2026-03-14 02:30:26 -04:00
|
|
|
|
audioSource.time = EditorManager.instance.songInformation.songTime +
|
|
|
|
|
|
EditorManager.instance.songInformation.offset;
|
2025-07-10 11:22:04 +08:00
|
|
|
|
if (isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
Trail.FreezeAllTrails(!isPlaying);
|
|
|
|
|
|
audioSource.Play();
|
|
|
|
|
|
}
|
2025-05-30 19:37:54 +08:00
|
|
|
|
else PauseMusic();
|
2025-02-14 22:04:21 -05:00
|
|
|
|
}
|
2026-03-14 02:30:26 -04:00
|
|
|
|
|
2025-06-21 23:09:34 +08:00
|
|
|
|
public IEnumerator PlayBackMusic()
|
|
|
|
|
|
{
|
2025-07-13 11:55:44 +08:00
|
|
|
|
float startt = audioSource.time - EditorManager.instance.songInformation.offset;
|
2025-06-21 23:09:34 +08:00
|
|
|
|
PlayMusic();
|
|
|
|
|
|
yield return new WaitUntil(() => Keyboard.current.rightAltKey.wasReleasedThisFrame);
|
2025-07-13 11:55:44 +08:00
|
|
|
|
audioSource.time = startt + EditorManager.instance.songInformation.offset;
|
2025-06-21 23:09:34 +08:00
|
|
|
|
PauseMusic();
|
|
|
|
|
|
}
|
2026-03-14 02:30:26 -04:00
|
|
|
|
|
2025-02-14 22:04:21 -05:00
|
|
|
|
public void PauseMusic()
|
|
|
|
|
|
{
|
2026-03-14 02:30:26 -04:00
|
|
|
|
if (isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorManager.instance.songInformation.songTime =
|
|
|
|
|
|
audioSource.time - EditorManager.instance.songInformation.offset;
|
|
|
|
|
|
}
|
2025-02-14 22:04:21 -05:00
|
|
|
|
isPlaying = false;
|
|
|
|
|
|
audioSource.Pause();
|
2025-07-10 11:22:04 +08:00
|
|
|
|
Trail.FreezeAllTrails(!isPlaying);
|
2025-02-14 22:04:21 -05:00
|
|
|
|
}
|
2025-02-28 20:08:00 +08:00
|
|
|
|
|
2025-02-14 22:04:21 -05:00
|
|
|
|
public void StopMusic()
|
|
|
|
|
|
{
|
|
|
|
|
|
isPlaying = false;
|
|
|
|
|
|
EditorManager.instance.songInformation.songTime = 0;
|
|
|
|
|
|
audioSource.Stop();
|
|
|
|
|
|
}
|
2026-03-14 02:30:26 -04:00
|
|
|
|
#endregion
|
2025-02-14 22:04:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|