SelectPrefab.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ToggleGroup toggleGroup;
  34. private bool isRight;
  35. private void Awake()
  36. {
  37. selectButton.onClick.AddListener(SelectButtonOnClick);
  38. toggleGroup = GetComponent<ToggleGroup>();
  39. }
  40. private void Start()
  41. {
  42. int number = 0;
  43. for (int i = 0; i < answers.Length; i++)
  44. {
  45. if (answers[i])
  46. {
  47. number += 1;
  48. }
  49. }
  50. answerPrefabList = new List<Toggle>();
  51. for (int i = 0; i < answerTexts.Length; i++)
  52. {
  53. answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
  54. answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
  55. answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
  56. if (number == 1)
  57. {
  58. answerPrefabList[i].group = toggleGroup;
  59. }
  60. }
  61. }
  62. private void ToggleOnSelect(bool value)
  63. {
  64. isRight = false;
  65. for (int i = 0; i < answerPrefabList.Count; i++)
  66. {
  67. if (answerPrefabList[i].isOn != answers[i])
  68. {
  69. return;
  70. }
  71. }
  72. isRight = true;
  73. }
  74. private void SelectButtonOnClick()
  75. {
  76. Debug.Log(isRight);
  77. selectCallBack?.Invoke(isRight);
  78. }
  79. /// <summary>
  80. /// 初始化
  81. /// </summary>
  82. public void SelectPrefabCreateInit(Action<bool> callBack)
  83. {
  84. selectCallBack = callBack;
  85. }
  86. }
粤ICP备19079148号