Files
ichni_Official/Assets/Scripts/Wwise/SongPlayer.cs

147 lines
4.7 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using Event = AK.Wwise.Event;
namespace Ichni
{
2025-06-06 10:14:55 -04:00
public class SongPlayer : SerializedMonoBehaviour
2025-06-03 02:42:28 -04:00
{
2025-07-08 14:28:40 -04:00
public AudioManager audioManager;
2025-06-03 02:42:28 -04:00
public Event PlayMusicEvent; // 播放背景音乐的事件
public Event ResumeMusicEvent; // 恢复播放背景音乐的事件
public Event PauseMusicEvent; // 暂停播放背景音乐的事件
public Event StopMusicEvent; // 停止播放背景音乐的事件
2025-07-08 14:28:40 -04:00
private uint _playingId;
2025-06-03 02:42:28 -04:00
public float songTimeSegment = 0;
public float pauseTimeSegment;
private float duration;
private float recordedSongSeg;
public float judgeOffset = 0;
private void Update()
{
2025-07-08 14:28:40 -04:00
if (audioManager.isLoading)
{
return;
}
if (audioManager.isDelaying)
2025-06-03 02:42:28 -04:00
{
songTimeSegment += Time.deltaTime;
2025-08-11 14:04:06 -04:00
//songTimeSegment = Mathf.Max(songTimeSegment, 0); // 确保时间段不为负数
2025-07-08 14:28:40 -04:00
if (songTimeSegment >= 0)
{
audioManager.isDelaying = false;
2025-08-11 14:04:06 -04:00
songTimeSegment = 0; // 延迟结束后,时间段归零
GameManager.instance.audioManager.songPlayer.PlaySong();
2025-07-08 14:28:40 -04:00
}
2025-06-03 02:42:28 -04:00
}
else
{
if (GameManager.instance.isDebugging)
{
return;
}
2025-07-08 14:28:40 -04:00
if (audioManager.isFinished)
2025-06-03 02:42:28 -04:00
{
songTimeSegment = recordedSongSeg;
2025-07-08 14:28:40 -04:00
2025-06-03 02:42:28 -04:00
return;
}
2025-07-08 14:28:40 -04:00
if (audioManager.isPlaying)
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
float currentSongSegment = PlaySegment() / 1000f - (judgeOffset / 1000f);
if (recordedSongSeg < currentSongSegment)
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
songTimeSegment = currentSongSegment;
recordedSongSeg = currentSongSegment;
2025-06-03 02:42:28 -04:00
}
}
2025-07-08 14:28:40 -04:00
else if (audioManager.isPausing)
2025-06-03 02:42:28 -04:00
{
2025-08-11 14:04:06 -04:00
songTimeSegment = pauseTimeSegment;
2025-06-03 02:42:28 -04:00
}
}
}
[Button]
2025-06-06 10:14:55 -04:00
public void PlaySong()
2025-06-03 02:42:28 -04:00
{
_playingId = PlayMusicEvent.Post(gameObject,
2025-08-11 14:04:06 -04:00
(uint)AkCallbackType.AK_EnableGetMusicPlayPosition |
(uint)AkCallbackType.AK_MusicSyncEntry |
(uint)AkCallbackType.AK_MusicSyncExit,
2025-06-03 02:42:28 -04:00
OnMusicEvent, null);
2025-07-08 14:28:40 -04:00
audioManager.isPlaying = true;
audioManager.isPausing = false;
audioManager.isFinished = false;
2025-06-03 02:42:28 -04:00
}
[Button]
2025-06-06 10:14:55 -04:00
public void PauseSong()
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
pauseTimeSegment = songTimeSegment;
2025-07-08 14:28:40 -04:00
audioManager.isPlaying = false;
audioManager.isPausing = true;
2025-06-03 02:42:28 -04:00
PauseMusicEvent.Post(gameObject);
Time.timeScale = 0;
}
[Button]
2025-06-06 10:14:55 -04:00
public void ResumeSong()
2025-06-03 02:42:28 -04:00
{
Time.timeScale = 1;
2025-07-08 14:28:40 -04:00
audioManager.isPlaying = true;
audioManager.isPausing = false;
2025-06-03 02:42:28 -04:00
ResumeMusicEvent.Post(gameObject);
}
[Button]
2025-06-06 10:14:55 -04:00
public void StopSong()
2025-06-03 02:42:28 -04:00
{
2025-07-08 14:28:40 -04:00
audioManager.isPlaying = false;
audioManager.isPausing = false;
2025-06-03 02:42:28 -04:00
StopMusicEvent.Post(gameObject);
}
2025-08-11 14:04:06 -04:00
2025-06-03 02:42:28 -04:00
private void OnMusicEvent(object in_cookie, AkCallbackType in_type, AkCallbackInfo in_info)
{
2025-08-11 14:04:06 -04:00
Debug.Log(in_type + " " + in_info);
if (in_type == AkCallbackType.AK_MusicSyncEntry)
2025-06-03 02:42:28 -04:00
{
2025-08-11 14:04:06 -04:00
if (in_info is AkMusicSyncCallbackInfo musicInfo)
2025-06-03 02:42:28 -04:00
{
2025-08-22 14:54:40 -04:00
GameManager.instance.songInformation.songLength = musicInfo.segmentInfo_iActiveDuration / 1000f;
2025-08-11 14:04:06 -04:00
InformationTransistor.instance.songLength = musicInfo.segmentInfo_iActiveDuration / 1000f;
InformationTransistor.instance.bpm = GameManager.instance.songInformation.bpm;
2025-06-03 02:42:28 -04:00
}
}
2025-08-11 14:04:06 -04:00
if (in_type == AkCallbackType.AK_MusicSyncExit)
{
audioManager.isFinished = true;
GameManager.instance.summaryPageCanvas.SetUpSummary();
GameManager.instance.summaryPageCanvas.FadeIn();
}
2025-06-03 02:42:28 -04:00
}
2025-08-11 14:04:06 -04:00
2025-06-03 02:42:28 -04:00
int PlaySegment()
{
AkSegmentInfo segmentInfo = new AkSegmentInfo();
AkSoundEngine.GetPlayingSegmentInfo(_playingId, segmentInfo,true);
return segmentInfo.iCurrentPosition;
}
}
}