using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class ChoiceQuesition : MonoBehaviour { Toggle[] toggles; [SerializeField] private int answer; private int currentAnswer; public Button button; public UnityEvent trueEvent; public UnityEvent falseEvent; private void Start() { toggles = GetComponentsInChildren(); for (int i = 0; i < toggles.Length; i++) { int index = i; toggles[index].onValueChanged.AddListener((bool b) => { if (toggles[index].isOn) { currentAnswer = index+1; } }); } button.onClick.AddListener(CheckAnswer); } public void CheckAnswer() { if (currentAnswer == answer) { Debug.Log("回答正确"); trueEvent.Invoke(); } else { Debug.Log("回答错误"); falseEvent.Invoke(); } } }