feature: change playing sound property

This commit is contained in:
2026-04-23 19:32:06 +08:00
parent e0ab4e4f7e
commit 769f5f3d2e
5 changed files with 242 additions and 43 deletions
@@ -179,6 +179,92 @@ namespace OCES.Audio
this.m_sfxSystem.Stop(audioId);
}
/// <summary>
/// 设置指定音频ID的音量
/// </summary>
/// <param name="audioId">音频ID</param>
/// <param name="targetVolume">目标音量 (0.0 - 1.0)</param>
public void SetVolume(uint audioId, float targetVolume)
{
if (targetVolume is < 0 or > 1)
{
Debug.LogWarning($"[AudioSystem] Volume '{targetVolume}' is out of range [0, 1].");
return;
}
this.m_sfxSystem.SetVolume(audioId, targetVolume);
}
/// <summary>
/// 设置指定音频ID的音量
/// </summary>
/// <param name="audioId">音频ID</param>
/// <param name="targetVolume">目标音量 (0.0 - 1.0)</param>
public void SetVolume(int audioId, float targetVolume)
{
SetVolume((uint)audioId, targetVolume);
}
/// <summary>
/// 设置指定音频ID的音高
/// </summary>
/// <param name="audioId">音频ID</param>
/// <param name="targetPitch">目标音高 (通常 1.0 为正常音高, -3 ~ 3)</param>
public void SetPitch(uint audioId, float targetPitch)
{
if (targetPitch is < -3 or > 3)
{
Debug.LogWarning($"[AudioSystem] Pitch '{targetPitch}' is out of range [-3, 3].");
return;
}
this.m_sfxSystem.SetPitch(audioId, targetPitch);
}
/// <summary>
/// 设置指定音频ID的音高
/// </summary>
/// <param name="audioId">音频ID</param>
/// <param name="targetPitch">目标音高 (通常 1.0 为正常音高)</param>
public void SetPitch(int audioId, float targetPitch)
{
SetPitch((uint)audioId, targetPitch);
}
/// <summary>
/// 重置指定音频ID的音量为默认值
/// </summary>
/// <param name="audioId">音频ID</param>
public void ResetVolume(uint audioId)
{
this.m_sfxSystem.ResetVolume(audioId);
}
/// <summary>
/// 重置指定音频ID的音量为默认值
/// </summary>
/// <param name="audioId">音频ID</param>
public void ResetVolume(int audioId)
{
ResetVolume((uint)audioId);
}
/// <summary>
/// 重置指定音频ID的音高为默认值
/// </summary>
/// <param name="audioId">音频ID</param>
public void ResetPitch(uint audioId)
{
this.m_sfxSystem.ResetPitch(audioId);
}
/// <summary>
/// 重置指定音频ID的音高为默认值
/// </summary>
/// <param name="audioId">音频ID</param>
public void ResetPitch(int audioId)
{
ResetPitch((uint)audioId);
}
// ─────────────────────────────────────────────
// 初始化
// ─────────────────────────────────────────────
@@ -99,6 +99,62 @@ namespace OCES.Audio
}
}
internal void SetVolume(uint audioId, float targetVolume)
{
List<ActiveSound> targets = this.m_activeSounds.FindAll(activeSound => activeSound.AudioObject.Id == audioId);
foreach (ActiveSound target in targets)
{
target.Volume = targetVolume;
if (target.Source)
{
target.Source.volume = targetVolume;
}
}
}
internal void SetPitch(uint audioId, float targetPitch)
{
List<ActiveSound> targets = this.m_activeSounds.FindAll(activeSound => activeSound.AudioObject.Id == audioId);
foreach (ActiveSound target in targets)
{
target.Pitch = targetPitch;
if (target.Source)
{
target.Source.pitch = targetPitch;
}
}
}
internal void ResetVolume(uint audioId)
{
double now = Time.realtimeSinceStartupAsDouble * 1000.0;
List<ActiveSound> targets = this.m_activeSounds.FindAll(activeSound => activeSound.AudioObject.Id == audioId);
foreach (ActiveSound target in targets)
{
float defaultVolume = this.m_volumeStepResolver.ResolveVolume(target.AudioObject, now);
target.Volume = defaultVolume;
if (target.Source)
{
target.Source.volume = defaultVolume;
}
}
}
internal void ResetPitch(uint audioId)
{
double now = Time.realtimeSinceStartupAsDouble * 1000.0;
List<ActiveSound> targets = this.m_activeSounds.FindAll(activeSound => activeSound.AudioObject.Id == audioId);
foreach (ActiveSound target in targets)
{
float defaultPitch = this.m_pitchStepResolver.ResolvePitch(target.AudioObject, now);
target.Pitch = defaultPitch;
if (target.Source != null)
{
target.Source.pitch = defaultPitch;
}
}
}
// ─────────────────────────────────────────────
// 节流 & 调度入口
// ─────────────────────────────────────────────
+36
View File
@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using OCES.Audio;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class SetPropertyBind : MonoBehaviour
{
public InputField inputField;
public float targetValue;
public void SetVolume()
{
uint.TryParse(this.inputField.text, out uint audioId);
AudioSystem.Instance.SetVolume(audioId, this.targetValue);
}
public void SetPitch()
{
uint.TryParse(this.inputField.text, out uint audioId);
AudioSystem.Instance.SetPitch(audioId, this.targetValue);
}
public void ResetVolume()
{
uint.TryParse(this.inputField.text, out uint audioId);
AudioSystem.Instance.ResetVolume(audioId);
}
public void ResetPitch()
{
uint.TryParse(this.inputField.text, out uint audioId);
AudioSystem.Instance.ResetPitch(audioId);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5820ebe13451f4706bb41024f117f10b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: