QuestionItemToggle.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class QuestionItemToggle : MonoBehaviour
  7. {
  8. public event Action<QuestionItemToggle, bool> OnValueChange;
  9. private Toggle m_Toggle;
  10. private Text m_Answer;
  11. private string m_AnswerText;
  12. private int m_ID;
  13. private Image m_RightImage;
  14. private Image m_ErrorImage;
  15. public string AnswerText
  16. {
  17. get { return m_AnswerText; }
  18. set
  19. {
  20. if (value != null)
  21. {
  22. m_AnswerText = value;
  23. m_Answer.text = m_AnswerText;
  24. }
  25. }
  26. }
  27. public int ID
  28. {
  29. get { return m_ID; }
  30. set { m_ID = value; }
  31. }
  32. private void Awake()
  33. {
  34. m_Toggle = GetComponent<Toggle>();
  35. m_Answer = transform.Find("Label").GetComponent<Text>();
  36. m_RightImage = transform.Find("RightImage").GetComponent<Image>();
  37. m_ErrorImage = transform.Find("ErrorImage").GetComponent<Image>();
  38. m_ErrorImage.gameObject.SetActive(false);
  39. m_RightImage.gameObject.SetActive(false);
  40. m_Toggle.onValueChanged.AddListener(OnToggleValueChanged);
  41. }
  42. public void Init()
  43. {
  44. }
  45. private void OnToggleValueChanged(bool value)
  46. {
  47. if (OnValueChange != null)
  48. {
  49. OnValueChange(this, value);
  50. }
  51. }
  52. /// <summary>
  53. /// 判断答案是否正确
  54. /// </summary>
  55. /// <param name="istrue"></param>
  56. public void JudgeAnswer(bool istrue)
  57. {
  58. m_ErrorImage.gameObject.SetActive(!istrue);
  59. m_RightImage.gameObject.SetActive(istrue);
  60. }
  61. public void ChangeState(bool value)
  62. {
  63. if (!value)
  64. {
  65. m_ErrorImage.gameObject.SetActive(false);
  66. m_RightImage.gameObject.SetActive(false);
  67. }
  68. }
  69. public void SetInteractable(bool value)
  70. {
  71. m_Toggle.interactable = value;
  72. }
  73. private void OnDisable()
  74. {
  75. m_ErrorImage.gameObject.SetActive(false);
  76. m_RightImage.gameObject.SetActive(false);
  77. m_Toggle.isOn = false;
  78. }
  79. }