61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using UnityEngine;
|
|
|
|
namespace Cielonos.MainGame
|
|
{
|
|
/// <summary>
|
|
/// 挂载在敌人等 GameObject 上的局部节奏追踪组件,用来追踪该物体上独立播放的 Wwise Segment。
|
|
/// </summary>
|
|
[AddComponentMenu("Cielonos/Rhythm/LocalRhythmTracker")]
|
|
public class LocalRhythmTracker : MonoBehaviour, ILocalRhythmTracker
|
|
{
|
|
public MusicBeatData CurrentBeatData { get; private set; }
|
|
public float CurrentSongTime { get; private set; }
|
|
public bool IsPlaying { get; private set; }
|
|
|
|
private float pendingSyncTime = -1f;
|
|
|
|
private void OnEnable()
|
|
{
|
|
// 自动将自身注册到全局物体映射表中,使 Wwise 回调能识别此 GameObject
|
|
MusicBeatSystem.RegisterRhythmGameObject(gameObject);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
MusicBeatSystem.UnregisterRhythmGameObject(gameObject);
|
|
}
|
|
|
|
public void OnSegmentTransitioned(MusicBeatData localBeatData)
|
|
{
|
|
CurrentBeatData = localBeatData;
|
|
CurrentSongTime = localBeatData != null ? localBeatData.audioStartOffset : 0f;
|
|
IsPlaying = localBeatData != null;
|
|
pendingSyncTime = -1f;
|
|
Debug.Log($"[LocalRhythmTracker] Segment transitioned on '{gameObject.name}' to '{localBeatData?.name}'");
|
|
}
|
|
|
|
public void ReceiveSyncBeat(float musicPositionSec, float beatDuration)
|
|
{
|
|
float offset = CurrentBeatData != null ? CurrentBeatData.audioStartOffset : 0f;
|
|
pendingSyncTime = musicPositionSec + offset;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (CurrentBeatData == null) return;
|
|
|
|
if (pendingSyncTime >= 0f)
|
|
{
|
|
CurrentSongTime = pendingSyncTime;
|
|
pendingSyncTime = -1f;
|
|
IsPlaying = true;
|
|
}
|
|
|
|
if (IsPlaying)
|
|
{
|
|
CurrentSongTime += Time.deltaTime;
|
|
}
|
|
}
|
|
}
|
|
}
|