| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEditor;
- using System;
- public class SelectPrefab : MonoBehaviour
- {
- [Header("题目主题")]
- public Text nameText;
- [Header("题目文本")]
- public Text text;
- [Header("答案文本数组")]
- public string[] answerTexts;
- [Header("答案数组")]
- public bool[] answers;
- [Header("是否显示认知提问对话框")]
- public bool textActive;
- [Header("是否上传该题目数据")]
- public bool upData;
- [Header(" ")]
- public Transform answerParent;
- public Toggle answerPrefab;
- /// <summary>
- /// 提交按钮
- /// </summary>
- public Button selectButton;
- /// <summary>
- /// 关闭按钮
- /// </summary>
- public Button closeButton;
- /// <summary>
- /// 高级认知正确/错误
- /// </summary>
- public GameObject trueText1;
- public GameObject falseText1;
- /// <summary>
- /// 低级认知正确/错误
- /// </summary>
- public GameObject trueText2;
- public GameObject falseText2;
- /// <summary>
- /// 练习题目答题提示
- /// </summary>
- public Text tipsText;
- /// <summary>
- /// 答案UI预制体列表
- /// </summary>
- private List<Toggle> answerPrefabList;
- /// <summary>
- /// 提交回调
- /// </summary>
- private Action<QuestionData> selectCallBack;
- private ToggleGroup toggleGroup;
- private bool isRight;
- /// <summary>
- /// 题目音频文件
- /// </summary>
- private AudioSource audioSource;
- private void Awake()
- {
- selectButton.onClick.AddListener(SelectButtonOnClick);
- closeButton.onClick.AddListener(CloseButtonOnClick);
- audioSource = GetComponent<AudioSource>();
- toggleGroup = GetComponent<ToggleGroup>();
- }
- private void OnEnable()
- {
- text.gameObject.SetActive(true);
- tipsText.transform.parent.gameObject.SetActive(false);
- }
- private void Start()
- {
- int number = 0;
- for (int i = 0; i < answers.Length; i++)
- {
- if (answers[i])
- {
- number += 1;
- }
- }
- answerPrefabList = new List<Toggle>();
- for (int i = 0; i < answerTexts.Length; i++)
- {
- answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
- answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
- answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
- if (number == 1)
- {
- answerPrefabList[i].group = toggleGroup;
- }
- }
- }
- private void ToggleOnSelect(bool value)
- {
- isRight = false;
- for (int i = 0; i < answerPrefabList.Count; i++)
- {
- if (answerPrefabList[i].isOn != answers[i])
- {
- return;
- }
- }
- isRight = true;
- }
- /// <summary>
- /// 选完确认按钮
- /// </summary>
- private void SelectButtonOnClick()
- {
- audioSource.enabled = false;
- if (textActive)
- {
- text.gameObject.SetActive(false);
- closeButton.gameObject.SetActive(true);
- if (answers[0])
- {
- if (isRight)
- {
- trueText1.gameObject.SetActive(true);
- }
- else
- {
- falseText1.gameObject.SetActive(true);
- }
- }
- else
- {
- if (isRight)
- {
- trueText2.gameObject.SetActive(true);
- }
- else
- {
- falseText2.gameObject.SetActive(true);
- }
- }
- }
- else
- {
- if (upData)
- {
- CloseButtonOnClick();
- }
- else
- {
- closeButton.gameObject.SetActive(true);
- if (isRight)
- {
- tipsText.text = "答案正确";
- }
- else
- {
- string d = "";
- for (int i = 0; i < answerTexts.Length; i++)
- {
- if (answers[i])
- {
- d += answerTexts[i];
- }
- }
- tipsText.text = $"答案错误,正确答案为{d}选项";
- }
- tipsText.transform.parent.gameObject.SetActive(true);
- }
- //selectCallBack?.Invoke(isRight);
- }
- }
- /// <summary>
- /// 关闭按钮
- /// </summary>
- private void CloseButtonOnClick()
- {
- QuestionData questionData = new QuestionData();
- questionData.text = text.text;
- for (int i = 0; i < answerPrefabList.Count; i++)
- {
- if (i == 0)
- {
- questionData.selectIndex = answerPrefabList[i].isOn.ToString();
- }
- else
- {
- questionData.selectIndex += "," + answerPrefabList[i].isOn.ToString();
- }
- }
- questionData.isRight = isRight;
- questionData.isUpData = upData;
- selectCallBack?.Invoke(questionData);
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void SelectPrefabCreateInit(Action<QuestionData> callBack)
- {
- selectCallBack = callBack;
- text.gameObject.SetActive(true);
- audioSource.enabled = true;
- trueText1.gameObject.SetActive(false);
- falseText1.gameObject.SetActive(false);
- trueText2.gameObject.SetActive(false);
- falseText2.gameObject.SetActive(false);
- }
- }
|