fix: bg音乐缓入+语音打断+代码碎片清理
- PlayHomeBg/PlayExperimentBg 增加 FadeInBg 缓入协程(1.5s) - TouchPositionHandler 使用静态共享 AudioSource 避免语音重叠 - 清理 PageController 中因多次编辑产生的代码碎片和括号混乱
This commit is contained in:
@@ -49,7 +49,7 @@ public class TouchPositionHandler : MonoBehaviour
|
||||
|
||||
if (voiceClip != null)
|
||||
{
|
||||
AudioSource.PlayClipAtPoint(voiceClip, transform.position);
|
||||
PlaySharedVoice();
|
||||
Debug.Log($"[TouchPosition] 播放语音: {voiceClip.name}");
|
||||
}
|
||||
|
||||
@@ -67,4 +67,20 @@ public class TouchPositionHandler : MonoBehaviour
|
||||
Debug.Log($"[TouchPosition] {name} → {nextTouchObject.name} 显示");
|
||||
}
|
||||
}
|
||||
|
||||
private static AudioSource _sharedVoice;
|
||||
|
||||
private void PlaySharedVoice()
|
||||
{
|
||||
if (voiceClip == null) return;
|
||||
if (_sharedVoice == null)
|
||||
{
|
||||
var go = GameObject.Find("[Managers]");
|
||||
if (go != null) _sharedVoice = go.AddComponent<AudioSource>();
|
||||
else { var g = new GameObject("SharedVoice"); _sharedVoice = g.AddComponent<AudioSource>(); }
|
||||
}
|
||||
_sharedVoice.Stop();
|
||||
_sharedVoice.clip = voiceClip;
|
||||
_sharedVoice.Play();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user