SelectPrefab.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEditor;
  6. using System;
  7. public class SelectPrefab : MonoBehaviour
  8. {
  9. [Header("题目主题")]
  10. public Text nameText;
  11. [Header("题目文本")]
  12. public Text text;
  13. [Header("答案文本数组")]
  14. public string[] answerTexts;
  15. [Header("答案数组")]
  16. public bool[] answers;
  17. [Header("是否显示认知提问对话框")]
  18. public bool textActive;
  19. [Header("是否上传该题目数据")]
  20. public bool upData;
  21. [Header(" ")]
  22. public Transform answerParent;
  23. public Toggle answerPrefab;
  24. /// <summary>
  25. /// 提交按钮
  26. /// </summary>
  27. public Button selectButton;
  28. /// <summary>
  29. /// 关闭按钮
  30. /// </summary>
  31. public Button closeButton;
  32. /// <summary>
  33. /// 高级认知正确/错误
  34. /// </summary>
  35. public GameObject trueText1;
  36. public GameObject falseText1;
  37. /// <summary>
  38. /// 低级认知正确/错误
  39. /// </summary>
  40. public GameObject trueText2;
  41. public GameObject falseText2;
  42. /// <summary>
  43. /// 练习题目答题提示
  44. /// </summary>
  45. public Text tipsText;
  46. /// <summary>
  47. /// 答案UI预制体列表
  48. /// </summary>
  49. private List<Toggle> answerPrefabList;
  50. /// <summary>
  51. /// 提交回调
  52. /// </summary>
  53. private Action<QuestionData> selectCallBack;
  54. private ToggleGroup toggleGroup;
  55. private bool isRight;
  56. /// <summary>
  57. /// 题目音频文件
  58. /// </summary>
  59. private AudioSource audioSource;
  60. private void Awake()
  61. {
  62. selectButton.onClick.AddListener(SelectButtonOnClick);
  63. closeButton.onClick.AddListener(CloseButtonOnClick);
  64. audioSource = GetComponent<AudioSource>();
  65. toggleGroup = GetComponent<ToggleGroup>();
  66. }
  67. private void OnEnable()
  68. {
  69. text.gameObject.SetActive(true);
  70. tipsText.transform.parent.gameObject.SetActive(false);
  71. }
  72. private void Start()
  73. {
  74. int number = 0;
  75. for (int i = 0; i < answers.Length; i++)
  76. {
  77. if (answers[i])
  78. {
  79. number += 1;
  80. }
  81. }
  82. answerPrefabList = new List<Toggle>();
  83. for (int i = 0; i < answerTexts.Length; i++)
  84. {
  85. answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
  86. answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
  87. answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
  88. if (number == 1)
  89. {
  90. answerPrefabList[i].group = toggleGroup;
  91. }
  92. }
  93. }
  94. private void ToggleOnSelect(bool value)
  95. {
  96. isRight = false;
  97. for (int i = 0; i < answerPrefabList.Count; i++)
  98. {
  99. if (answerPrefabList[i].isOn != answers[i])
  100. {
  101. return;
  102. }
  103. }
  104. isRight = true;
  105. }
  106. /// <summary>
  107. /// 选完确认按钮
  108. /// </summary>
  109. private void SelectButtonOnClick()
  110. {
  111. audioSource.enabled = false;
  112. if (textActive)
  113. {
  114. text.gameObject.SetActive(false);
  115. closeButton.gameObject.SetActive(true);
  116. if (answers[0])
  117. {
  118. if (isRight)
  119. {
  120. trueText1.gameObject.SetActive(true);
  121. }
  122. else
  123. {
  124. falseText1.gameObject.SetActive(true);
  125. }
  126. }
  127. else
  128. {
  129. if (isRight)
  130. {
  131. trueText2.gameObject.SetActive(true);
  132. }
  133. else
  134. {
  135. falseText2.gameObject.SetActive(true);
  136. }
  137. }
  138. }
  139. else
  140. {
  141. if (upData)
  142. {
  143. CloseButtonOnClick();
  144. }
  145. else
  146. {
  147. closeButton.gameObject.SetActive(true);
  148. if (isRight)
  149. {
  150. tipsText.text = "答案正确";
  151. }
  152. else
  153. {
  154. string d = "";
  155. for (int i = 0; i < answerTexts.Length; i++)
  156. {
  157. if (answers[i])
  158. {
  159. d += answerTexts[i];
  160. }
  161. }
  162. tipsText.text = $"答案错误,正确答案为{d}选项";
  163. }
  164. tipsText.transform.parent.gameObject.SetActive(true);
  165. }
  166. //selectCallBack?.Invoke(isRight);
  167. }
  168. }
  169. /// <summary>
  170. /// 关闭按钮
  171. /// </summary>
  172. private void CloseButtonOnClick()
  173. {
  174. QuestionData questionData = new QuestionData();
  175. questionData.text = text.text;
  176. for (int i = 0; i < answerPrefabList.Count; i++)
  177. {
  178. if (i == 0)
  179. {
  180. questionData.selectIndex = answerPrefabList[i].isOn.ToString();
  181. }
  182. else
  183. {
  184. questionData.selectIndex += "," + answerPrefabList[i].isOn.ToString();
  185. }
  186. }
  187. questionData.isRight = isRight;
  188. questionData.isUpData = upData;
  189. selectCallBack?.Invoke(questionData);
  190. }
  191. /// <summary>
  192. /// 初始化
  193. /// </summary>
  194. public void SelectPrefabCreateInit(Action<QuestionData> callBack)
  195. {
  196. selectCallBack = callBack;
  197. text.gameObject.SetActive(true);
  198. audioSource.enabled = true;
  199. trueText1.gameObject.SetActive(false);
  200. falseText1.gameObject.SetActive(false);
  201. trueText2.gameObject.SetActive(false);
  202. falseText2.gameObject.SetActive(false);
  203. }
  204. }
粤ICP备19079148号