1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class QuestionItemToggle : MonoBehaviour
- {
- public event Action<QuestionItemToggle, bool> OnValueChange;
- private Toggle m_Toggle;
- private Text m_Answer;
- private string m_AnswerText;
- private int m_ID;
- private Image m_RightImage;
- private Image m_ErrorImage;
- public string AnswerText
- {
- get { return m_AnswerText; }
- set
- {
- if (value != null)
- {
- m_AnswerText = value;
- m_Answer.text = m_AnswerText;
- }
- }
- }
- public int ID
- {
- get { return m_ID; }
- set { m_ID = value; }
- }
- private void Awake()
- {
- m_Toggle = GetComponent<Toggle>();
- m_Answer = transform.Find("Label").GetComponent<Text>();
- m_RightImage = transform.Find("RightImage").GetComponent<Image>();
- m_ErrorImage = transform.Find("ErrorImage").GetComponent<Image>();
- m_ErrorImage.gameObject.SetActive(false);
- m_RightImage.gameObject.SetActive(false);
- m_Toggle.onValueChanged.AddListener(OnToggleValueChanged);
- }
- public void Init()
- {
- }
- private void OnToggleValueChanged(bool value)
- {
- if (OnValueChange != null)
- {
- OnValueChange(this, value);
- }
- }
- /// <summary>
- /// 判断答案是否正确
- /// </summary>
- /// <param name="istrue"></param>
- public void JudgeAnswer(bool istrue)
- {
- m_ErrorImage.gameObject.SetActive(!istrue);
- m_RightImage.gameObject.SetActive(istrue);
- }
- public void ChangeState(bool value)
- {
- if (!value)
- {
- m_ErrorImage.gameObject.SetActive(false);
- m_RightImage.gameObject.SetActive(false);
- }
- }
- public void SetInteractable(bool value)
- {
- m_Toggle.interactable = value;
- }
- private void OnDisable()
- {
- m_ErrorImage.gameObject.SetActive(false);
- m_RightImage.gameObject.SetActive(false);
- m_Toggle.isOn = false;
- }
- }
|