feat: 完整音频系统 - 首页/实验背景音乐+woman语音序列
- PageController新增_homeBgClip/_experimentBgClip/_womanClips数组 - Start()创建AudioSource循环播放背景音乐 - ShowPage控制: Home播首页bg, 离开首页停止bg - ActivateExperimentObjects: 停止首页bg→播实验bg→顺序播放3段woman语音 - PlayWomanAudioSequence()协程: 每段播完后等0.5s再播下一段
This commit is contained in:
@@ -49,108 +49,43 @@ public class PageController : MonoBehaviour
|
||||
{
|
||||
Debug.Log("=== PageController 初始化开始 ===");
|
||||
|
||||
// 背景音乐 AudioSource
|
||||
_bgAudioSource = gameObject.AddComponent<AudioSource>();
|
||||
_bgAudioSource.loop = true;
|
||||
_bgAudioSource.playOnAwake = false;
|
||||
|
||||
// 绑定首页按钮
|
||||
if (_btnStart != null)
|
||||
{
|
||||
_btnStart.onClick.AddListener(OnStartClicked);
|
||||
Debug.Log("PageController: _btnStart 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnStart 未赋值!");
|
||||
if (_btnStart != null) { _btnStart.onClick.AddListener(OnStartClicked); Debug.Log("PageController: _btnStart 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnStart 未赋值!");
|
||||
|
||||
if (_btnExit != null)
|
||||
{
|
||||
_btnExit.onClick.AddListener(OnExitClicked);
|
||||
Debug.Log("PageController: _btnExit 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnExit 未赋值!");
|
||||
if (_btnExit != null) { _btnExit.onClick.AddListener(OnExitClicked); Debug.Log("PageController: _btnExit 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnExit 未赋值!");
|
||||
|
||||
// 绑定模式选择页按钮
|
||||
if (_btnBack != null)
|
||||
{
|
||||
_btnBack.onClick.AddListener(OnModeBackClicked);
|
||||
Debug.Log("PageController: _btnBack 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnBack 未赋值!");
|
||||
if (_btnBack != null) { _btnBack.onClick.AddListener(OnModeBackClicked); Debug.Log("PageController: _btnBack 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnBack 未赋值!");
|
||||
|
||||
if (_btnPracticeMode != null)
|
||||
{
|
||||
_btnPracticeMode.onClick.AddListener(() => OnModeSelected(ExperimentMode.Learn));
|
||||
Debug.Log("PageController: _btnPracticeMode 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnPracticeMode 未赋值!");
|
||||
if (_btnPracticeMode != null) { _btnPracticeMode.onClick.AddListener(() => OnModeSelected(ExperimentMode.Learn)); Debug.Log("PageController: _btnPracticeMode 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnPracticeMode 未赋值!");
|
||||
|
||||
if (_btnExamMode != null)
|
||||
{
|
||||
_btnExamMode.onClick.AddListener(() => OnModeSelected(ExperimentMode.Exam));
|
||||
Debug.Log("PageController: _btnExamMode 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnExamMode 未赋值!");
|
||||
if (_btnExamMode != null) { _btnExamMode.onClick.AddListener(() => OnModeSelected(ExperimentMode.Exam)); Debug.Log("PageController: _btnExamMode 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnExamMode 未赋值!");
|
||||
|
||||
// 绑定固定方式选择页按钮 —— 自动修复
|
||||
if (_btnFixationBack == null && _fixationPanel != null)
|
||||
{
|
||||
var btnBack = _fixationPanel.transform.Find("BtnBack");
|
||||
if (btnBack != null)
|
||||
{
|
||||
_btnFixationBack = btnBack.GetComponent<Button>();
|
||||
Debug.Log("PageController: 自动绑定 FixationPanel/BtnBack(_btnFixationBack 原为空)");
|
||||
}
|
||||
}
|
||||
if (_btnFixationBack != null)
|
||||
{
|
||||
_btnFixationBack.onClick.AddListener(OnFixationBackClicked);
|
||||
Debug.Log("PageController: _btnFixationBack 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnFixationBack 未找到!请在 Inspector 赋值或确保 FixationPanel 下有名为 BtnBack 的子物体。");
|
||||
if (_btnFixationBack == null && _fixationPanel != null) { var fb = _fixationPanel.transform.Find("BtnBack"); if (fb != null) { _btnFixationBack = fb.GetComponent<Button>(); Debug.Log("PageController: 自动绑定 FixationPanel/BtnBack"); } }
|
||||
if (_btnFixationBack != null) { _btnFixationBack.onClick.AddListener(OnFixationBackClicked); Debug.Log("PageController: _btnFixationBack 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnFixationBack 未找到!");
|
||||
|
||||
if (_btnLimbFixation != null)
|
||||
{
|
||||
_btnLimbFixation.onClick.AddListener(() => OnFixationSelected(FixationMethod.LimbFixation));
|
||||
Debug.Log("PageController: _btnLimbFixation 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnLimbFixation 未赋值!");
|
||||
if (_btnLimbFixation != null) { _btnLimbFixation.onClick.AddListener(() => OnFixationSelected(FixationMethod.LimbFixation)); Debug.Log("PageController: _btnLimbFixation 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnLimbFixation 未赋值!");
|
||||
|
||||
if (_btnSplintFixation != null)
|
||||
{
|
||||
_btnSplintFixation.onClick.AddListener(() => OnFixationSelected(FixationMethod.SplintFixation));
|
||||
Debug.Log("PageController: _btnSplintFixation 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnSplintFixation 未赋值!");
|
||||
if (_btnSplintFixation != null) { _btnSplintFixation.onClick.AddListener(() => OnFixationSelected(FixationMethod.SplintFixation)); Debug.Log("PageController: _btnSplintFixation 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnSplintFixation 未赋值!");
|
||||
|
||||
// 绑定实验页面按钮 —— 自动修复
|
||||
if (_btnExperimentBack == null && _experimentPanel != null)
|
||||
{
|
||||
var btnBack = _experimentPanel.transform.Find("Navbar/BtnBack");
|
||||
if (btnBack != null)
|
||||
{
|
||||
_btnExperimentBack = btnBack.GetComponent<Button>();
|
||||
Debug.Log("PageController: 自动绑定 ExperimentPanel/Navbar/BtnBack(_btnExperimentBack 原为空)");
|
||||
}
|
||||
}
|
||||
if (_btnExperimentBack != null)
|
||||
{
|
||||
_btnExperimentBack.onClick.AddListener(OnExperimentBackClicked);
|
||||
Debug.Log("PageController: _btnExperimentBack 绑定成功");
|
||||
}
|
||||
else
|
||||
Debug.LogError("PageController: _btnExperimentBack 未找到!请在 Inspector 赋值或确保 ExperimentPanel/Navbar 下有名为 BtnBack 的子物体。");
|
||||
if (_btnExperimentBack == null && _experimentPanel != null) { var eb = _experimentPanel.transform.Find("Navbar/BtnBack"); if (eb != null) { _btnExperimentBack = eb.GetComponent<Button>(); Debug.Log("PageController: 自动绑定 ExperimentPanel/Navbar/BtnBack"); } }
|
||||
if (_btnExperimentBack != null) { _btnExperimentBack.onClick.AddListener(OnExperimentBackClicked); Debug.Log("PageController: _btnExperimentBack 绑定成功"); }
|
||||
else Debug.LogError("PageController: _btnExperimentBack 未找到!");
|
||||
|
||||
// 初始化模式说明文字
|
||||
if (_modeDescText != null)
|
||||
{
|
||||
_modeDescText.text = _practiceModeDesc + "\n\n" + _examModeDesc;
|
||||
Debug.Log("PageController: _modeDescText 初始化完成");
|
||||
}
|
||||
if (_modeDescText != null) { _modeDescText.text = _practiceModeDesc + "\n\n" + _examModeDesc; }
|
||||
|
||||
// 初始显示首页
|
||||
ShowPage(Page.Home);
|
||||
Debug.Log("=== PageController 初始化完成 ===");
|
||||
}
|
||||
@@ -204,8 +139,41 @@ 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);
|
||||
if (_uiManager != null) _uiManager.enabled = (page == Page.Experiment);
|
||||
|
||||
// 背景音乐控制
|
||||
if (page == Page.Home) PlayHomeBg();
|
||||
else if (page != Page.Experiment) StopBgMusic();
|
||||
}
|
||||
|
||||
private void PlayHomeBg()
|
||||
{
|
||||
if (_homeBgClip != null && _bgAudioSource != null && _bgAudioSource.clip != _homeBgClip)
|
||||
{
|
||||
_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>
|
||||
@@ -335,27 +303,38 @@ public class PageController : MonoBehaviour
|
||||
{
|
||||
Debug.Log("PageController: [ActivateExperimentObjects] 显示实验物件");
|
||||
|
||||
// medicalBox
|
||||
PlayExperimentBg();
|
||||
StartCoroutine(PlayWomanAudioSequence());
|
||||
|
||||
var allObjects = FindObjectsOfType<GameObject>(true);
|
||||
foreach (var go in allObjects)
|
||||
{
|
||||
if (go.name == "medicalBox")
|
||||
{
|
||||
go.SetActive(true);
|
||||
Debug.Log("PageController: medicalBox 已显示");
|
||||
break;
|
||||
}
|
||||
if (go.name == "medicalBox") { go.SetActive(true); Debug.Log("PageController: medicalBox 已显示"); break; }
|
||||
}
|
||||
|
||||
// touchArray 的第一个球(point1)
|
||||
var touch = GameObject.Find("touchArray");
|
||||
if (touch != null && touch.transform.childCount > 0)
|
||||
if (touch != null && touch.transform.childCount > 0) { touch.transform.GetChild(0).gameObject.SetActive(true); Debug.Log("PageController: touchArray/point1 已显示"); }
|
||||
|
||||
var tipPanel = GameObject.Find("TipPanel");
|
||||
if (tipPanel != null) tipPanel.SetActive(true);
|
||||
var dialogue = GameObject.Find("DialogueBubble");
|
||||
if (dialogue != null) dialogue.SetActive(true);
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator PlayWomanAudioSequence()
|
||||
{
|
||||
if (_womanClips == null || _womanClips.Length == 0) yield break;
|
||||
var voiceSource = gameObject.AddComponent<AudioSource>();
|
||||
voiceSource.loop = false;
|
||||
voiceSource.playOnAwake = false;
|
||||
foreach (var clip in _womanClips)
|
||||
{
|
||||
var point1 = touch.transform.GetChild(0).gameObject;
|
||||
point1.SetActive(true);
|
||||
Debug.Log("PageController: touchArray/point1 已显示");
|
||||
var tipPanel = GameObject.Find("TipPanel"); if (tipPanel != null) tipPanel.SetActive(true);
|
||||
var dialogue = GameObject.Find("DialogueBubble"); if (dialogue != null) dialogue.SetActive(true); }
|
||||
if (clip == null) continue;
|
||||
voiceSource.clip = clip;
|
||||
voiceSource.Play();
|
||||
Debug.Log($"PageController: 播放woman语音: {clip.name}");
|
||||
yield return new WaitForSeconds(clip.length + 0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -388,4 +367,11 @@ public class PageController : MonoBehaviour
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
[Header("=== 音频 ===")]
|
||||
[SerializeField] private AudioClip _homeBgClip;
|
||||
[SerializeField] private AudioClip _experimentBgClip;
|
||||
[SerializeField] private AudioClip[] _womanClips;
|
||||
private AudioSource _bgAudioSource;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user