SelectPrefab.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. private void Awake()
  51. {
  52. selectButton.onClick.AddListener(SelectButtonOnClick);
  53. closeButton.onClick.AddListener(CloseButtonOnClick);
  54. toggleGroup = GetComponent<ToggleGroup>();
  55. }
  56. private void Start()
  57. {
  58. int number = 0;
  59. for (int i = 0; i < answers.Length; i++)
  60. {
  61. if (answers[i])
  62. {
  63. number += 1;
  64. }
  65. }
  66. answerPrefabList = new List<Toggle>();
  67. for (int i = 0; i < answerTexts.Length; i++)
  68. {
  69. answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
  70. answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
  71. answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
  72. if (number == 1)
  73. {
  74. answerPrefabList[i].group = toggleGroup;
  75. }
  76. }
  77. }
  78. private void ToggleOnSelect(bool value)
  79. {
  80. isRight = false;
  81. for (int i = 0; i < answerPrefabList.Count; i++)
  82. {
  83. if (answerPrefabList[i].isOn != answers[i])
  84. {
  85. return;
  86. }
  87. }
  88. isRight = true;
  89. }
  90. /// <summary>
  91. /// 选完确认按钮
  92. /// </summary>
  93. private void SelectButtonOnClick()
  94. {
  95. if (textActive)
  96. {
  97. closeButton.gameObject.SetActive(true);
  98. if (answers[0])
  99. {
  100. if (isRight)
  101. {
  102. trueText1.gameObject.SetActive(true);
  103. }
  104. else
  105. {
  106. falseText1.gameObject.SetActive(true);
  107. }
  108. }
  109. else
  110. {
  111. if (isRight)
  112. {
  113. trueText2.gameObject.SetActive(true);
  114. }
  115. else
  116. {
  117. falseText2.gameObject.SetActive(true);
  118. }
  119. }
  120. }
  121. else
  122. {
  123. selectCallBack?.Invoke(isRight);
  124. }
  125. }
  126. /// <summary>
  127. /// 关闭按钮
  128. /// </summary>
  129. private void CloseButtonOnClick()
  130. {
  131. selectCallBack?.Invoke(isRight);
  132. }
  133. /// <summary>
  134. /// 初始化
  135. /// </summary>
  136. public void SelectPrefabCreateInit(Action<bool> callBack)
  137. {
  138. selectCallBack = callBack;
  139. text.gameObject.SetActive(true);
  140. trueText1.gameObject.SetActive(false);
  141. falseText1.gameObject.SetActive(false);
  142. trueText2.gameObject.SetActive(false);
  143. falseText2.gameObject.SetActive(false);
  144. }
  145. }
粤ICP备19079148号