fix: 删除PageController中所有旧音频死代码—只留BGMController

旧PlayHomeBg/PlayExperimentBg/StopBgMusic/FadeInBg/PlayWomanAudioSequence
和对应字段全部删除,避免与BGMController静态方法共存
This commit is contained in:
yangbear
2026-07-01 22:10:30 +08:00
parent e1a9f90d3f
commit efce2422b8
-63
View File
@@ -321,73 +321,10 @@ public class PageController : MonoBehaviour
#endregion
[Header("=== 音频 ===")]
[SerializeField] private AudioClip _homeBgClip;
[SerializeField] private AudioClip _experimentBgClip;
[SerializeField] private AudioClip[] _womanClips;
private AudioSource _bgAudioSource;
private Coroutine _fadeCoroutine;
private void PlayHomeBg()
{
if (_homeBgClip == null) {
#if UNITY_EDITOR
_homeBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/homeBackgroundMusic.mp3");
#endif
if (_homeBgClip == null) return; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false;
_bgAudioSource.spatialBlend = 0f; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _homeBgClip;
_bgAudioSource.volume = 0.5f;
_bgAudioSource.Play();
Debug.Log("PageController: 播放首页背景音乐 vol=0.5 isPlaying=" + _bgAudioSource.isPlaying + " clip=" + (_bgAudioSource.clip!=null?_bgAudioSource.clip.name:"null"));
}
private void StopBgMusic()
{
if (_fadeCoroutine != null) { StopCoroutine(_fadeCoroutine); _fadeCoroutine = null; }
var allSrcs = gameObject.GetComponents<AudioSource>();
foreach (var s in allSrcs)
{
if (s == null) continue;
Debug.Log("PageController: 停止AudioSource clip=" + (s.clip != null ? s.clip.name : "null"));
s.Stop();
}
Debug.Log("PageController: 停止背景音乐 (共" + allSrcs.Length + "个AudioSource)");
}
private void PlayExperimentBg()
{
StopBgMusic();
if (_experimentBgClip == null) {
#if UNITY_EDITOR
_experimentBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/backgroundMusic.mp3");
#endif
if (_experimentBgClip == null) return; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false;
_bgAudioSource.spatialBlend = 0f; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _experimentBgClip;
_bgAudioSource.volume = 0f;
_bgAudioSource.Play();
if (_fadeCoroutine != null) StopCoroutine(_fadeCoroutine);
_fadeCoroutine = StartCoroutine(FadeInBg());
Debug.Log("PageController: 播放实验背景音乐");
}
private System.Collections.IEnumerator FadeInBg(float duration = 1.5f)
{
if (_bgAudioSource == null) yield break;
float targetVolume = _bgAudioSource.volume > 0.01f ? _bgAudioSource.volume : 0.5f;
Debug.Log("PageController: FadeInBg 目标音量=" + targetVolume + " spatialBlend=" + _bgAudioSource.spatialBlend);
_bgAudioSource.volume = 0f;
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
if (_bgAudioSource != null) _bgAudioSource.volume = Mathf.Lerp(0f, targetVolume, elapsed / duration);
yield return null;
}
if (_bgAudioSource != null) _bgAudioSource.volume = targetVolume;
}
}