f0a6b96da5
- 删除重复 Ground - 创建 [Managers] 空物体,挂载 GameManager + StepManager + ExperimentFlowCoordinator + AudioManager - Main Camera 挂载 InteractionSystem - 伤者 InjectedPerson 下创建 5 个交互子物体 (InjuryVisual/Swelling/Pain/VitalSigns/Neuro) - 创建绑带位置、踝关节、衬垫、夹板位置等空物体 - 道具摆放到位:伤者(2,0.05,5) 施救者(3,0,5) 鞋(2.7,0.15,5.5) 绷带/夹板/急救箱在旁 - InjuryLeg Tag 修正,Ground Tag 修正 - 全部新脚本同步导入 (ExperimentTypes/StepData/ExperimentConfig/StepManager/GameManager/ExperimentFlowCoordinator/InteractionSystem/DraggableObject/HighlightEffect/UIManager/ExamPopup/ScoreDisplay/VideoController/AudioManager/CharacterAnimController/StepAnimationHandler) - 旧脚本 (ExperimentController/ConfigLoader/AnimationController/HUDController/VideoPlayerController/PopupSystem/ModeSelectionUI) 全部兼容修复或 stub - 编译通过,0 错误 - Canvas 挂载 UIManager
93 lines
2.7 KiB
C#
93 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 音频管理器:管理背景音乐和步骤语音解说。
|
|
/// 挂在场景 GameObject 上,通过 AudioSource 组件播放。
|
|
/// </summary>
|
|
[RequireComponent(typeof(AudioSource))]
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
public static AudioManager Instance { get; private set; }
|
|
|
|
[Header("音频源")]
|
|
[SerializeField] private AudioSource _bgmSource;
|
|
[SerializeField] private AudioSource _voiceSource;
|
|
|
|
[Header("背景音乐")]
|
|
public AudioClip BGMClip;
|
|
[Range(0f, 1f)] public float BGMVolume = 0.3f;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null) { Destroy(gameObject); return; }
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
if (_bgmSource == null) _bgmSource = GetComponent<AudioSource>();
|
|
if (_voiceSource == null)
|
|
{
|
|
_voiceSource = gameObject.AddComponent<AudioSource>();
|
|
_voiceSource.playOnAwake = false;
|
|
_voiceSource.loop = false;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
PlayBGM();
|
|
}
|
|
|
|
public void PlayBGM()
|
|
{
|
|
if (_bgmSource == null || BGMClip == null) return;
|
|
_bgmSource.clip = BGMClip;
|
|
_bgmSource.volume = BGMVolume;
|
|
_bgmSource.loop = true;
|
|
_bgmSource.Play();
|
|
}
|
|
|
|
public void StopBGM()
|
|
{
|
|
if (_bgmSource != null) _bgmSource.Stop();
|
|
}
|
|
|
|
/// <summary>播放一步语音解说,自动打断前一条</summary>
|
|
public void PlayVoice(AudioClip clip, float volume = 1f)
|
|
{
|
|
if (_voiceSource == null || clip == null) return;
|
|
_voiceSource.Stop();
|
|
_voiceSource.clip = clip;
|
|
_voiceSource.volume = volume;
|
|
_voiceSource.Play();
|
|
}
|
|
|
|
/// <summary>停止语音</summary>
|
|
public void StopVoice()
|
|
{
|
|
if (_voiceSource != null) _voiceSource.Stop();
|
|
}
|
|
|
|
/// <summary>按文件名从 StreamingAssets 加载并播放语音</summary>
|
|
public void PlayVoiceFromFile(string fileName)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName)) return;
|
|
StartCoroutine(LoadAndPlayVoice(fileName));
|
|
}
|
|
|
|
private System.Collections.IEnumerator LoadAndPlayVoice(string fileName)
|
|
{
|
|
string path = System.IO.Path.Combine(Application.streamingAssetsPath, "Voice", fileName);
|
|
using var www = UnityEngine.Networking.UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV);
|
|
yield return www.SendWebRequest();
|
|
if (www.result == UnityEngine.Networking.UnityWebRequest.Result.Success)
|
|
{
|
|
var clip = UnityEngine.Networking.DownloadHandlerAudioClip.GetContent(www);
|
|
PlayVoice(clip);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"AudioManager: 加载语音失败 {path}");
|
|
}
|
|
}
|
|
}
|