using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEditor; using System; public class SelectPrefab : MonoBehaviour { [Header("播放顺序")] public int index; [Header("题目主题")] public string topicName; [Header("题目文本")] public string topicText; [Header("答案文本数组")] public string[] answerTexts; [Header("答案数组")] public bool[] answers; [Header(" ")] public Text nameText; public Text text; public Transform answerParent; public Toggle answerPrefab; public Button selectButton; /// /// 答案UI预制体列表 /// private List answerPrefabList; /// /// 提交回调 /// private Action selectCallBack; private ToggleGroup toggleGroup; private bool isRight; private void Awake() { selectButton.onClick.AddListener(SelectButtonOnClick); toggleGroup = GetComponent(); } private void Start() { int number = 0; for (int i = 0; i < answers.Length; i++) { if (answers[i]) { number += 1; } } answerPrefabList = new List(); for (int i = 0; i < answerTexts.Length; i++) { answerPrefabList.Add(Instantiate(answerPrefab, answerParent)); answerPrefabList[i].GetComponentInChildren().text = answerTexts[i]; answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect); if (number == 1) { answerPrefabList[i].group = toggleGroup; } } } private void ToggleOnSelect(bool value) { isRight = false; for (int i = 0; i < answerPrefabList.Count; i++) { if (answerPrefabList[i].isOn != answers[i]) { return; } } isRight = true; } private void SelectButtonOnClick() { Debug.Log(isRight); selectCallBack?.Invoke(isRight); } /// /// 初始化 /// public void SelectPrefabCreateInit(Action callBack) { selectCallBack = callBack; } }