添加音效 v0.1版

This commit is contained in:
2026-07-04 18:35:23 +08:00
parent 8a6721f22a
commit 8ae4469c72
37 changed files with 588 additions and 7 deletions
+24
View File
@@ -113,6 +113,30 @@ namespace GameFramework
PlaySFX(data);
}
/// <summary>
/// 直接通过 AudioClip 播放音效。
/// </summary>
public void PlaySFX(AudioClip clip, float volume = 1f)
{
if (clip == null) return;
sfxPlayer.PlayOneShot(clip, volume);
}
/// <summary>
/// 通过 Resources 路径加载并播放音效。
/// 路径相对于 Assets/audios/Resources/,例如 "SFX/Bell"
/// </summary>
public void PlaySFXFromResources(string resourcePath, float volume = 1f)
{
var clip = Resources.Load<AudioClip>(resourcePath);
if (clip == null)
{
Debug.LogWarning($"[AudioManager] SFX not found: {resourcePath}");
return;
}
PlaySFX(clip, volume);
}
public void SetSFXVolume(float volume)
{
sfxVolume = Mathf.Clamp01(volume);
+46
View File
@@ -0,0 +1,46 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace GameFramework
{
/// <summary>
/// 全局 UI 按钮音效处理器。
/// 挂到 Canvas 上,自动监听所有按钮的点击/悬停事件并播放音效。
/// 无需在每个按钮上单独添加组件。
/// </summary>
public class UIClickSoundHandler : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler
{
[Header("音效资源路径(相对于 Resources 文件夹)")]
[SerializeField] private string selectSFXPath = "SFX/UI_Click";
[SerializeField] private string clickSFXPath = "SFX/UI_Click";
[Header("音量")]
[SerializeField] private float selectVolume = 0.5f;
[SerializeField] private float clickVolume = 0.6f;
private void PlaySFX(string path, float volume)
{
if (AudioManager.Instance != null)
AudioManager.Instance.PlaySFXFromResources(path, volume);
}
public void OnPointerEnter(PointerEventData eventData)
{
// 只对按钮播放悬停音效
if (eventData.pointerEnter != null && eventData.pointerEnter.GetComponent<Button>() != null)
{
PlaySFX(selectSFXPath, selectVolume);
}
}
public void OnPointerDown(PointerEventData eventData)
{
// 只对按钮播放点击音效
if (eventData.pointerPress != null && eventData.pointerPress.GetComponent<Button>() != null)
{
PlaySFX(clickSFXPath, clickVolume);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: CytM5yuuAC+4h5kKmSUxrxML3yei5rN0+BflxvkJIAYWLnhdd2gv6Zw=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: