Question.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class Question : MonoBehaviour
  8. {
  9. public event Action<int, bool, Question> CalScoreQuestion;
  10. private GameObject m_ItemPrefab = null;
  11. private Text m_Question;
  12. private string m_QuestionText;
  13. private Text m_Title;
  14. private int m_RightID;
  15. private List<string> m_AnswerData = new List<string>();
  16. private List<QuestionItemToggle> m_AnswerList;
  17. private QuestionItemToggle m_CurrentAns;
  18. private int m_Score = 0;
  19. public List<string> AnswerData
  20. {
  21. get { return m_AnswerData; }
  22. set
  23. {
  24. if (m_AnswerData.Count > 0)
  25. {
  26. m_AnswerData = null;
  27. }
  28. m_AnswerData = value;
  29. }
  30. }
  31. public int RightID
  32. {
  33. get { return m_RightID; }
  34. set { m_RightID = value; }
  35. }
  36. public string QuestionText
  37. {
  38. get { return m_QuestionText; }
  39. set
  40. {
  41. if (value != null)
  42. {
  43. m_QuestionText = value;
  44. m_Question.text = m_QuestionText;
  45. }
  46. }
  47. }
  48. public int Score
  49. {
  50. get { return m_Score; }
  51. set { m_Score = value; }
  52. }
  53. void Awake()
  54. {
  55. m_CurrentAns = null;
  56. m_AnswerList = new List<QuestionItemToggle>();
  57. m_ItemPrefab = transform.Find("Canvas/Question/Panel/QuestionItem").gameObject;
  58. m_Question = transform.Find("Canvas/Question").GetComponent<Text>();
  59. m_Title = transform.Find("Canvas/Title").GetComponent<Text>();
  60. }
  61. private void Start()
  62. {
  63. //根据数据生成UI面板,当有数据后可使用
  64. for (int i = 0; i < AnswerData.Count; i++)
  65. {
  66. var item = GetQuestionItem();
  67. item.OnValueChange += OnClickAnswer;
  68. item.ID = i;
  69. item.AnswerText = AnswerData[i];
  70. item.Init();
  71. m_AnswerList.Add(item);
  72. }
  73. }
  74. private void OnClickAnswer(QuestionItemToggle item, bool value)
  75. {
  76. var isanswer = m_RightID == item.ID ? true : false;
  77. item.JudgeAnswer(isanswer);
  78. if (m_CurrentAns != null)
  79. {
  80. m_CurrentAns.ChangeState(value);
  81. }
  82. m_CurrentAns = item;
  83. if (value)
  84. {
  85. m_Title.gameObject.SetActive(true);
  86. m_Title.text = isanswer == true ? "回答正确" : "回答错误再试一次";
  87. }
  88. else
  89. {
  90. m_Title.gameObject.SetActive(false);
  91. }
  92. //计算玩家答对几道题
  93. if (null != CalScoreQuestion)
  94. {
  95. CalScoreQuestion(m_Score, isanswer, this);
  96. }
  97. SetToggleInteractable();
  98. }
  99. private QuestionItemToggle GetQuestionItem()
  100. {
  101. QuestionItemToggle item = null;
  102. var parenttra = m_Question.transform.Find("Panel");
  103. var obj = Instantiate(m_ItemPrefab, parenttra);
  104. obj.transform.localRotation = Quaternion.identity;
  105. obj.transform.localScale = new Vector3(1, 1, 1);
  106. obj.SetActive(true);
  107. item = obj.GetComponent<QuestionItemToggle>();
  108. return item;
  109. }
  110. /// <summary>
  111. /// 调用方法,将点击的结果返回到服务器
  112. /// </summary>
  113. private void SendAnswerMessage(string msg)
  114. {
  115. Debug.Log(msg);
  116. }
  117. private void SetToggleInteractable()
  118. {
  119. for (int i = 0; i < m_AnswerList.Count; i++)
  120. {
  121. m_AnswerList[i].SetInteractable(false);
  122. }
  123. }
  124. private void OnDisable()
  125. {
  126. for (int i = 0; i < m_AnswerList.Count; i++)
  127. {
  128. m_AnswerList[i].SetInteractable(true);
  129. }
  130. m_Title.gameObject.SetActive(false);
  131. }
  132. }