SelectPrefab.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 int index;
  11. [Header("题目主题")]
  12. public string topicName;
  13. [Header("题目文本")]
  14. public string topicText;
  15. [Header("答案文本数组")]
  16. public string[] answerTexts;
  17. [Header("答案数组")]
  18. public bool[] answers;
  19. [Header(" ")]
  20. public Text nameText;
  21. public Text text;
  22. public Transform answerParent;
  23. public Toggle answerPrefab;
  24. public Button selectButton;
  25. /// <summary>
  26. /// 答案UI预制体列表
  27. /// </summary>
  28. private List<Toggle> answerPrefabList;
  29. /// <summary>
  30. /// 提交回调
  31. /// </summary>
  32. private Action<bool> selectCallBack;
  33. private bool isRight;
  34. private void Awake()
  35. {
  36. selectButton.onClick.AddListener(SelectButtonOnClick);
  37. }
  38. private void Start()
  39. {
  40. answerPrefabList = new List<Toggle>();
  41. for (int i = 0; i < answerTexts.Length; i++)
  42. {
  43. answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
  44. answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
  45. answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
  46. }
  47. }
  48. private void ToggleOnSelect(bool value)
  49. {
  50. isRight = false;
  51. for (int i = 0; i < answerPrefabList.Count; i++)
  52. {
  53. if (answerPrefabList[i].isOn != answers[i])
  54. {
  55. return;
  56. }
  57. }
  58. isRight = true;
  59. }
  60. private void SelectButtonOnClick()
  61. {
  62. selectCallBack?.Invoke(isRight);
  63. }
  64. }
粤ICP备19079148号