using UnityEngine; using UnityEngine.UI; using QFramework; using System.Collections.Generic; using System; namespace QFramework { public class QuestionPanelData : UIPanelData { //试题信息 public QuestionInfo questionInfo; public Action answerCallBack; } public partial class QuestionPanel : UIPanel { /// /// 目前正确的Toggle /// public List m_CurrentRightToggle; /// /// 生成的所有的Toggle /// public List m_CurrentAllToggle; /// /// 目前题目类型 /// public string m_CurrentQuestionType; protected override void OnInit(IUIData uiData = null) { mData = uiData as QuestionPanelData ?? new QuestionPanelData(); } protected override void OnOpen(IUIData uiData = null) { mData = uiData as QuestionPanelData ?? new QuestionPanelData(); QuestionType.text = mData.questionInfo.QuestionType; Topic.text = mData.questionInfo.Topic; ConfirmBtn.onClick.AddListener(OnConfirmBtnClick); InitilOptions(mData.questionInfo, mData.questionInfo.QuestionType); } protected override void OnShow() { } protected override void OnHide() { } protected override void OnClose() { } /// /// 初始化选项 /// /// private void InitilOptions(QuestionInfo questionInfo, string type) { m_CurrentQuestionType = type; if (!string.IsNullOrEmpty(questionInfo.Options)) { var optionArr = questionInfo.Options.Split(';'); for (int i = 0; i < optionArr.Length; i++) { var tempItem = Instantiate(option.gameObject, OptionConent.transform); tempItem.SetActive(true); var toggle = tempItem.GetComponent(); tempItem.transform.Find("Label").GetComponent().text = optionArr[i]; if (questionInfo.Answer.Contains(optionArr[i].Split('、')[0])) m_CurrentRightToggle.Add(toggle); m_CurrentAllToggle.Add(toggle); toggle.onValueChanged.AddListener(isOn => { ToggleValuedChanged(toggle, isOn); }); } } else { for (int i = 0; i < 2; i++) { var tempItem = Instantiate(option.gameObject, OptionConent.transform); tempItem.SetActive(true); var toggle = tempItem.GetComponent(); if (i == 0) { tempItem.transform.Find("Label").GetComponent().text = "对"; if (questionInfo.Answer == "对") m_CurrentRightToggle.Add(toggle); } else { tempItem.transform.Find("Label").GetComponent().text = "错"; if (questionInfo.Answer == "错") m_CurrentRightToggle.Add(toggle); } m_CurrentAllToggle.Add(toggle); toggle.onValueChanged.AddListener( isOn => { ToggleValuedChanged(toggle, isOn); } ); } } } private void ToggleValuedChanged(Toggle toggle,bool isOn) { if (m_CurrentQuestionType == "判断题" || m_CurrentQuestionType == "单选题") { var tmptoggle = toggle.GetComponent(); tmptoggle.group = OptionConent.GetComponent(); } } private void OnConfirmBtnClick() { bool result = true; foreach (var item in m_CurrentRightToggle) { if (item.isOn == false) { result = false; } } Debug.LogError(result); if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Practice) { if (result) { mData.answerCallBack?.Invoke(result); CloseSelf(); } else { ShowHighter(); } } else if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Exam) { mData.answerCallBack?.Invoke(result); CloseSelf(); } } private void ShowHighter() { foreach (var item in m_CurrentAllToggle) { if (m_CurrentRightToggle.Contains(item)) { item.transform.Find("Label").GetComponent().color = Color.green; } else { if (item.isOn) { item.transform.Find("Label").GetComponent().color = Color.red; } } } } } }