using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; using UnityEngine.UI; using Random = UnityEngine.Random; public class QuestionPanel : MonoBehaviour { [Header("回调事件")] public UnityEvent sureEvent; public UnityEvent errorEvent; [Header("配置项")] public bool isNeedChaos = true;//是否需要乱序选项 [Header("基本元素")] public GameObject togglePre; public GameObject inputPre; public Text title; public GameObject toggleArea; public Button answerSure; public GameObject errorTip; public GameObject questionPanel; protected List cells = new List(); private string _inputAnswer; private int _singleAnswer; private List _multipleAnswer = new List(); private QuestionData _questionData; private void Awake() { _questionData = GetComponent(); InitPanel(); } /// /// 初始化 /// void InitPanel() { questionPanel.SetActive(true); sureEvent.RemoveAllListeners(); errorEvent.RemoveAllListeners(); foreach (var cell in cells) { Destroy(cell.gameObject); } cells.Clear(); _inputAnswer = ""; _singleAnswer = -1; _multipleAnswer.Clear(); errorTip.gameObject.SetActive(false); title.gameObject.SetActive(true); toggleArea.gameObject.SetActive(true); answerSure.gameObject.SetActive(true); } /// /// 创建一个题目 /// /// 题目标签 public void InitQuestion(string tags) { if (!_questionData.QuestionInfos.ContainsKey(tags)) { Debug.LogError("没有该题目标签" + ":" + tags); return; } InitPanel(); QuestionInfo info = _questionData.QuestionInfos[tags]; title.text = info.title; if (info.type == QuestionType.multiple) { MultipleInfo toggleInfo = info as MultipleInfo; _multipleAnswer = toggleInfo.answer; if (toggleInfo.options.Count <= 4) { toggleArea.GetComponent().cellSize = new Vector2(600,70); } else if(toggleInfo.options.Count <= 8) { toggleArea.GetComponent().cellSize = new Vector2(300,50); } else { Debug.LogError("超出最大上限8个"); } for (int i = 0; i < toggleInfo.options.Count; i++) { GameObject obj = GameObject.Instantiate(togglePre); QuestionCell questionCell = obj.GetComponent(); cells.Add(questionCell); questionCell.transform.SetParent(toggleArea.transform); questionCell.InitToggle(toggleInfo.options[i],i); } answerSure.onClick.AddListener(AnswerCallBackByMultiple); } else if (info.type == QuestionType.Single) { SingleInfo toggleInfo = info as SingleInfo; _singleAnswer = toggleInfo.answer; if (toggleInfo.options.Count <= 4) { toggleArea.GetComponent().cellSize = new Vector2(600,70); } else if(toggleInfo.options.Count <= 8) { toggleArea.GetComponent().cellSize = new Vector2(300,50); } else { Debug.LogError("超出最大上限8个"); } ToggleGroup group = toggleArea.GetComponent(); for (int i = 0; i < toggleInfo.options.Count; i++) { GameObject obj = GameObject.Instantiate(togglePre); QuestionCell questionCell = obj.GetComponent(); cells.Add(questionCell); questionCell.transform.SetParent(toggleArea.transform); questionCell.InitToggle(toggleInfo.options[i],i); questionCell.GetComponent().group = group; } answerSure.onClick.AddListener(AnswerCallBackBySingle); } else if (info.type == QuestionType.Input) { InputfieldInfo toggleInfo = info as InputfieldInfo; _inputAnswer = toggleInfo.answer; GameObject obj = GameObject.Instantiate(inputPre); QuestionCell questionCell = obj.GetComponent(); cells.Add(questionCell); questionCell.transform.SetParent(transform); answerSure.onClick.AddListener(AnswerCallBackByInputField); } if(!isNeedChaos) return; //随机选项 if (info.type != QuestionType.Input) { for (int i = 0; i < cells.Count; i++) { int rand = Random.Range(0, cells.Count); QuestionCell temp = cells[i]; cells[i] = cells[rand]; cells[rand] = temp; } foreach (var btn in cells) { btn.transform.SetParent(transform); btn.transform.SetParent(toggleArea.transform); } } } /// /// 单选题回调 /// public void AnswerCallBackBySingle() { foreach (QuestionCell toggle in cells) { if (toggle.GetComponent().isOn && toggle.id != _singleAnswer) { //错误 ErrorCallBack(); return; } } //正确 SureCallBack(); questionPanel.SetActive(false); } /// /// 多选题回调 /// public void AnswerCallBackByMultiple() { foreach (QuestionCell toggle in cells) { if (toggle.GetComponent().isOn && !_multipleAnswer.Contains(toggle.id)) { //错误 ErrorCallBack(); return; } } //正确 SureCallBack(); questionPanel.SetActive(false); } /// /// 填空题回调 /// public void AnswerCallBackByInputField() { if (_inputAnswer == cells[0].inputText.text) { //正确 SureCallBack(); questionPanel.SetActive(false); } else { //错误 ErrorCallBack(); } } void ErrorDisappear() { errorTip.gameObject.SetActive(false); } void SureCallBack() { sureEvent?.Invoke(); } void ErrorCallBack() { errorTip.gameObject.SetActive(true); errorTip.transform.localScale = new Vector3(0, 1, 1); errorTip.transform.DOScaleX(1, 0.5f); Invoke("ErrorDisappear",3f); errorEvent?.Invoke(); } }