MusicBeat
This commit is contained in:
@@ -236,4 +236,20 @@ namespace SLSUtilities.General
|
||||
list.Insert(low, newItem);
|
||||
}
|
||||
}
|
||||
|
||||
public static partial class ListExtension
|
||||
{
|
||||
public static void Invoke(this IList<PrioritizedAction> list)
|
||||
{
|
||||
foreach (PrioritizedAction pAction in list)
|
||||
{
|
||||
pAction.Invoke();
|
||||
|
||||
if (pAction.ShouldRemove)
|
||||
{
|
||||
list.Remove(pAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
55
Assets/Scripts/SLSUtilities/General/Timer.cs
Normal file
55
Assets/Scripts/SLSUtilities/General/Timer.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SLSUtilities.General
|
||||
{
|
||||
public class Timer
|
||||
{
|
||||
public float duration;
|
||||
public float currentTime;
|
||||
public bool isInfinite;
|
||||
public float Percentage => duration > 0 ? Mathf.Clamp01(currentTime / duration) : 1f;
|
||||
public virtual bool IsCompleted => currentTime >= duration;
|
||||
|
||||
public List<PrioritizedAction> onComplete;
|
||||
|
||||
public Timer(float duration, bool isInfinite = false)
|
||||
{
|
||||
this.duration = duration;
|
||||
this.isInfinite = isInfinite;
|
||||
currentTime = 0f;
|
||||
onComplete = new List<PrioritizedAction>();
|
||||
}
|
||||
|
||||
public virtual void Update(float deltaTime)
|
||||
{
|
||||
if (!IsCompleted)
|
||||
{
|
||||
currentTime += deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isInfinite)
|
||||
{
|
||||
onComplete.Invoke();
|
||||
onComplete.Clear(); // 确保只调用一次
|
||||
}
|
||||
else
|
||||
{
|
||||
onComplete.Invoke();
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Reset(float newDuration = -1f)
|
||||
{
|
||||
currentTime = 0f;
|
||||
if(newDuration >= 0f)
|
||||
{
|
||||
duration = newDuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/SLSUtilities/General/Timer.cs.meta
Normal file
2
Assets/Scripts/SLSUtilities/General/Timer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3220a5be96b1f614c99f0d1a32a2dae6
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using AK.Wwise;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine;
|
||||
using Event = AK.Wwise.Event;
|
||||
|
||||
namespace SLSUtilities.WwiseAssistance
|
||||
{
|
||||
@@ -13,24 +12,52 @@ namespace SLSUtilities.WwiseAssistance
|
||||
public Event playMusicEvent; // 播放背景音乐的事件
|
||||
public Event stopMusicEvent; // 停止播放背景音乐的事件
|
||||
|
||||
/// <summary>
|
||||
/// 是否被外部系统(如 MusicBeatSystem)覆盖中
|
||||
/// </summary>
|
||||
[ShowInInspector, ReadOnly]
|
||||
private bool isOverridden;
|
||||
|
||||
/// <summary>
|
||||
/// 最后一次播放请求的音乐 State 名称,用于恢复
|
||||
/// </summary>
|
||||
private string lastMusicStateName;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
PlayMusic("NormalMusic");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放指定 State 的背景音乐。被覆盖期间调用会被静默忽略
|
||||
/// </summary>
|
||||
public void PlayMusic(string musicStateName)
|
||||
{
|
||||
//if (baseMusicDictionary.ContainsKey(musicStateName))
|
||||
if (isOverridden)
|
||||
{
|
||||
stopMusicEvent.Post(gameObject);
|
||||
//baseMusicDictionary[musicStateName].SetValue();
|
||||
playMusicEvent.Post(gameObject);
|
||||
lastMusicStateName = musicStateName;
|
||||
return;
|
||||
}
|
||||
|
||||
lastMusicStateName = musicStateName;
|
||||
stopMusicEvent.Post(gameObject);
|
||||
playMusicEvent.Post(gameObject);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 停止背景音乐
|
||||
/// </summary>
|
||||
public void StopMusic()
|
||||
{
|
||||
stopMusicEvent.Post(gameObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置覆盖状态。被覆盖期间,常规 PlayMusic 调用将被静默忽略
|
||||
/// </summary>
|
||||
public void SetOverride(bool overridden)
|
||||
{
|
||||
isOverridden = overridden;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user