fix: bg音乐缓入+语音打断+代码碎片清理

- PlayHomeBg/PlayExperimentBg 增加 FadeInBg 缓入协程(1.5s)
- TouchPositionHandler 使用静态共享 AudioSource 避免语音重叠
- 清理 PageController 中因多次编辑产生的代码碎片和括号混乱
This commit is contained in:
yangbear
2026-07-01 21:18:34 +08:00
parent 3a83140801
commit f68e675022
2 changed files with 63 additions and 34 deletions
+46 -33
View File
@@ -122,7 +122,7 @@ public class PageController : MonoBehaviour
if (_uiManager != null)
_uiManager.enabled = (page == Page.Experiment);
// 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg
if (page == Page.Home) PlayHomeBg(); // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg
if (page == Page.Home) PlayHomeBg();
Debug.Log($"PageController(编辑器预览): 切换到 [{page}]");
}
@@ -140,38 +140,6 @@ public class PageController : MonoBehaviour
if (_fixationPanel != null) _fixationPanel.SetActive(page == Page.Fixation);
if (_experimentPanel != null) _experimentPanel.SetActive(page == Page.Experiment);
if (_uiManager != null) _uiManager.enabled = (page == Page.Experiment);
// 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg
}
private void PlayHomeBg()
{
if (_homeBgClip != null && _bgAudioSource != null)
{
_bgAudioSource.clip = _homeBgClip;
_bgAudioSource.Play();
Debug.Log("PageController: 播放首页背景音乐");
}
}
private void StopBgMusic()
{
if (_bgAudioSource != null && _bgAudioSource.isPlaying)
{
_bgAudioSource.Stop();
Debug.Log("PageController: 停止背景音乐");
}
}
private void PlayExperimentBg()
{
StopBgMusic();
if (_experimentBgClip != null && _bgAudioSource != null)
{
_bgAudioSource.clip = _experimentBgClip;
_bgAudioSource.Play();
Debug.Log("PageController: 播放实验背景音乐");
}
}
/// <summary>
@@ -351,4 +319,49 @@ public class PageController : MonoBehaviour
[SerializeField] private AudioClip _experimentBgClip;
[SerializeField] private AudioClip[] _womanClips;
private AudioSource _bgAudioSource;
private void PlayHomeBg()
{
if (_homeBgClip == null) return;
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; }
_bgAudioSource.clip = _homeBgClip;
_bgAudioSource.Play();
StartCoroutine(FadeInBg());
Debug.Log("PageController: 播放首页背景音乐");
}
private void StopBgMusic()
{
if (_bgAudioSource != null && _bgAudioSource.isPlaying)
{
_bgAudioSource.Stop();
Debug.Log("PageController: 停止背景音乐");
}
}
private void PlayExperimentBg()
{
StopBgMusic();
if (_experimentBgClip == null) return;
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; }
_bgAudioSource.clip = _experimentBgClip;
_bgAudioSource.Play();
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;
_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;
}
}