| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 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;
- /// <summary>
- /// 答案UI预制体列表
- /// </summary>
- private List<Toggle> answerPrefabList;
- /// <summary>
- /// 提交回调
- /// </summary>
- private Action<bool> selectCallBack;
- private bool isRight;
- private void Awake()
- {
- selectButton.onClick.AddListener(SelectButtonOnClick);
- }
- private void Start()
- {
- answerPrefabList = new List<Toggle>();
- for (int i = 0; i < answerTexts.Length; i++)
- {
- answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
- answerPrefabList[i].GetComponentInChildren<Text>().text = answerTexts[i];
- answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
- }
- }
- 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()
- {
- selectCallBack?.Invoke(isRight);
- }
- }
|