SelectPrefab.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Transform answerParent;
  21. public Toggle answerPrefab;
  22. /// <summary>
  23. /// 提交按钮
  24. /// </summary>
  25. public Button selectButton;
  26. /// <summary>
  27. /// 关闭按钮
  28. /// </summary>
  29. public Button closeButton;
  30. /// <summary>
  31. /// 高级认知正确/错误
  32. /// </summary>
  33. public GameObject trueText1;
  34. public GameObject falseText1;
  35. /// <summary>
  36. /// 低级认知正确/错误
  37. /// </summary>
  38. public GameObject trueText2;
  39. public GameObject falseText2;
  40. /// <summary>
  41. /// 答案UI预制体列表
  42. /// </summary>
  43. private List<Toggle> answerPrefabList;
  44. /// <summary>
  45. /// 提交回调
  46. /// </summary>
  47. private Action<bool> selectCallBack;
  48. private ToggleGroup toggleGroup;
  49. private bool isRight;
  50. /// <summary>
  51. /// 题目音频文件
  52. /// </summary>
  53. private AudioSource audioSource;
  54. private void Awake()
  55. {
  56. selectButton.onClick.AddListener(SelectButtonOnClick);
  57. closeButton.onClick.AddListener(CloseButtonOnClick);
  58. audioSource = GetComponent<AudioSource>();
  59. toggleGroup = GetComponent<ToggleGroup>();
  60. }
  61. private void Start()
  62. {
  63. int number = 0;
  64. for (int i = 0; i < answers.Length; i++)
  65. {
  66. if (answers[i])
  67. {
  68. number += 1;
  69. }
  70. }
  71. answerPrefabList = new List<Toggle>();
  72. for (int i = 0; i < answerTexts.Length; i++)
  73. {
  74. answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
  75. answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
  76. answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
  77. if (number == 1)
  78. {
  79. answerPrefabList[i].group = toggleGroup;
  80. }
  81. }
  82. }
  83. private void ToggleOnSelect(bool value)
  84. {
  85. isRight = false;
  86. for (int i = 0; i < answerPrefabList.Count; i++)
  87. {
  88. if (answerPrefabList[i].isOn != answers[i])
  89. {
  90. return;
  91. }
  92. }
  93. isRight = true;
  94. }
  95. /// <summary>
  96. /// 选完确认按钮
  97. /// </summary>
  98. private void SelectButtonOnClick()
  99. {
  100. text.gameObject.SetActive(false);
  101. audioSource.enabled = false;
  102. if (textActive)
  103. {
  104. closeButton.gameObject.SetActive(true);
  105. if (answers[0])
  106. {
  107. if (isRight)
  108. {
  109. trueText1.gameObject.SetActive(true);
  110. }
  111. else
  112. {
  113. falseText1.gameObject.SetActive(true);
  114. }
  115. }
  116. else
  117. {
  118. if (isRight)
  119. {
  120. trueText2.gameObject.SetActive(true);
  121. }
  122. else
  123. {
  124. falseText2.gameObject.SetActive(true);
  125. }
  126. }
  127. }
  128. else
  129. {
  130. selectCallBack?.Invoke(isRight);
  131. }
  132. }
  133. /// <summary>
  134. /// 关闭按钮
  135. /// </summary>
  136. private void CloseButtonOnClick()
  137. {
  138. selectCallBack?.Invoke(isRight);
  139. }
  140. /// <summary>
  141. /// 初始化
  142. /// </summary>
  143. public void SelectPrefabCreateInit(Action<bool> callBack)
  144. {
  145. selectCallBack = callBack;
  146. text.gameObject.SetActive(true);
  147. audioSource.enabled = true;
  148. trueText1.gameObject.SetActive(false);
  149. falseText1.gameObject.SetActive(false);
  150. trueText2.gameObject.SetActive(false);
  151. falseText2.gameObject.SetActive(false);
  152. }
  153. }
粤ICP备19079148号