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
|
|
|
{
|
|
|
|
|
private uint _playingId;
|
|
|
|
|
|
|
|
|
|
public Event PlayMusicEvent; // 播放背景音乐的事件
|
|
|
|
|
public Event ResumeMusicEvent; // 恢复播放背景音乐的事件
|
|
|
|
|
public Event PauseMusicEvent; // 暂停播放背景音乐的事件
|
|
|
|
|
public Event StopMusicEvent; // 停止播放背景音乐的事件
|
|
|
|
|
|
|
|
|
|
public bool isDelaying = false;
|
|
|
|
|
public bool isPlaying = false; // 是否正在播放音乐
|
|
|
|
|
public bool isPausing = false; // 是否正在暂停音乐
|
|
|
|
|
public bool isFinished = false;
|
|
|
|
|
|
|
|
|
|
public float songTimeSegment = 0;
|
|
|
|
|
public float pauseTimeSegment;
|
|
|
|
|
private float duration;
|
|
|
|
|
private float recordedSongSeg;
|
|
|
|
|
|
|
|
|
|
public float judgeOffset = 0;
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (isDelaying)
|
|
|
|
|
{
|
|
|
|
|
songTimeSegment += Time.deltaTime;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (GameManager.instance.isDebugging)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isFinished)
|
|
|
|
|
{
|
|
|
|
|
songTimeSegment = recordedSongSeg;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isPlaying)
|
|
|
|
|
{
|
|
|
|
|
songTimeSegment = PlaySegment() / 1000f + (judgeOffset / 1000f);
|
|
|
|
|
if (songTimeSegment > 0)
|
|
|
|
|
{
|
|
|
|
|
recordedSongSeg = songTimeSegment;
|
|
|
|
|
}
|
|
|
|
|
else if (songTimeSegment == 0 && recordedSongSeg > 0)
|
|
|
|
|
{
|
|
|
|
|
isFinished = true;
|
|
|
|
|
songTimeSegment = recordedSongSeg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Debug.Log($"Song Time Segment: {songTimeSegment}, Recorded Segment: {recordedSongSeg}");
|
|
|
|
|
}
|
|
|
|
|
else if (isPausing)
|
|
|
|
|
{
|
|
|
|
|
songTimeSegment = pauseTimeSegment / 1000f + (judgeOffset / 1000f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Button]
|
2025-06-06 10:14:55 -04:00
|
|
|
public void PlaySong()
|
2025-06-03 02:42:28 -04:00
|
|
|
{
|
|
|
|
|
_playingId = PlayMusicEvent.Post(gameObject,
|
|
|
|
|
(uint)AkCallbackType.AK_EnableGetMusicPlayPosition | (uint)AkCallbackType.AK_MusicSyncAll,
|
|
|
|
|
OnMusicEvent, null);
|
|
|
|
|
|
|
|
|
|
isPlaying = true;
|
|
|
|
|
isPausing = false;
|
|
|
|
|
isFinished = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Button]
|
2025-06-06 10:14:55 -04:00
|
|
|
public void PauseSong()
|
2025-06-03 02:42:28 -04:00
|
|
|
{
|
|
|
|
|
pauseTimeSegment = PlaySegment();
|
|
|
|
|
isPlaying = false;
|
|
|
|
|
isPausing = true;
|
|
|
|
|
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;
|
|
|
|
|
isPlaying = true;
|
|
|
|
|
isPausing = false;
|
|
|
|
|
ResumeMusicEvent.Post(gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Button]
|
2025-06-06 10:14:55 -04:00
|
|
|
public void StopSong()
|
2025-06-03 02:42:28 -04:00
|
|
|
{
|
|
|
|
|
isPlaying = false;
|
|
|
|
|
isPausing = false;
|
|
|
|
|
StopMusicEvent.Post(gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMusicEvent(object in_cookie, AkCallbackType in_type, AkCallbackInfo in_info)
|
|
|
|
|
{
|
|
|
|
|
if (in_info is AkMusicSyncCallbackInfo)
|
|
|
|
|
{
|
|
|
|
|
if (in_type == AkCallbackType.AK_EndOfEvent)
|
|
|
|
|
{
|
|
|
|
|
isFinished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_type is AkCallbackType.AK_MusicSyncExit)
|
|
|
|
|
{
|
|
|
|
|
//GameManager.gameManager.OpenSummary();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PlaySegment()
|
|
|
|
|
{
|
|
|
|
|
AkSegmentInfo segmentInfo = new AkSegmentInfo();
|
|
|
|
|
AkSoundEngine.GetPlayingSegmentInfo(_playingId, segmentInfo,true);
|
|
|
|
|
|
|
|
|
|
return segmentInfo.iCurrentPosition;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|