QuestionControl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using SC.XR.Unity.Module_InputSystem;
  7. /*
  8. * 答题模块的控制面板
  9. */
  10. public class QuestionControl : MonoBehaviour
  11. {
  12. private GameObject m_Prefab;
  13. private GameObject m_SucessPanel;
  14. private Text m_SucessText;
  15. private int m_CurrentScore;
  16. private int m_RightQuestion;
  17. //已经回答的题目
  18. private List<Question> m_AlreadyQuestion;
  19. //所有题目
  20. private List<Question> m_Questions;
  21. /// <summary>
  22. /// 是否向服务器发送数据
  23. /// </summary>
  24. [SerializeField]
  25. private bool IsSendMsgToServer;
  26. ///// <summary>
  27. ///// 等待太阳系现时间
  28. ///// </summary>
  29. //[SerializeField]
  30. //private float m_WaitP2GalaxTime;
  31. //太阳系
  32. public GameObject m_P2Galax;
  33. private void OnEnable()
  34. {
  35. //ChangeInputDeviceType(InputDeviceType.Head);
  36. }
  37. private void Awake()
  38. {
  39. DataManager.Instance.Init();
  40. }
  41. private void Start()
  42. {
  43. m_CurrentScore = 0;
  44. m_RightQuestion = 0;
  45. m_SucessPanel = transform.Find("Sucess").gameObject;
  46. m_SucessText = m_SucessPanel.transform.Find("Text").GetComponent<Text>();
  47. m_Prefab = transform.Find("Question").gameObject;
  48. m_Prefab.SetActive(false);
  49. m_AlreadyQuestion = new List<Question>();
  50. m_Questions = new List<Question>();
  51. CraetQuestions();
  52. }
  53. public void Update()
  54. {
  55. }
  56. /// <summary>
  57. /// 根据服务器数据创建问题UI面板
  58. /// </summary>
  59. private void CraetQuestions()
  60. {
  61. var data = DataManager.Instance.QuesDataMgr.QuestionList;
  62. for (int i = 0; i < data.Count; i++)
  63. {
  64. var obj = Instantiate(m_Prefab) as GameObject;
  65. obj.SetActive(true);
  66. var question = obj.AddComponent<Question>();
  67. question.transform.SetParent(this.transform);
  68. question.transform.localPosition = data[i].position;
  69. question.transform.localRotation = Quaternion.Euler(data[i].rotation);
  70. question.transform.localScale = data[i].scale;
  71. question.QuestionText = data[i].question;
  72. question.AnswerData.AddRange(data[i].Answer);
  73. question.RightID = data[i].RightAnswer;
  74. question.Score = data[i].score;
  75. question.CalScoreQuestion += CalScore;
  76. m_Questions.Add(question);
  77. }
  78. }
  79. /// <summary>
  80. /// 计算玩家获得的分数
  81. /// </summary>
  82. /// <param name="score"></param>
  83. /// <param name="isright"></param>
  84. /// <param name="question"></param>
  85. private void CalScore(int score, bool isright, Question question)
  86. {
  87. if (!m_AlreadyQuestion.Contains(question))
  88. {
  89. m_AlreadyQuestion.Add(question);
  90. if (isright)
  91. {
  92. m_CurrentScore += score;
  93. m_RightQuestion += 1;
  94. }
  95. }
  96. if (m_AlreadyQuestion.Count >= DataManager.Instance.QuesDataMgr.QuestionList.Count)
  97. {
  98. m_SucessPanel.SetActive(true);
  99. string str = String.Format("{0}{1}{2}{3}{4}", "恭喜你答对", m_RightQuestion.ToString(), "道题,获得", m_CurrentScore.ToString(), "积分");
  100. m_SucessText.text = str;
  101. //发送信息到服务器
  102. if (IsSendMsgToServer)
  103. {
  104. SendQuestionInfor questionInfor = new SendQuestionInfor();
  105. questionInfor.sn = DeviceSN.Instance.SendSerialBy16();
  106. questionInfor.correct = m_RightQuestion;
  107. questionInfor.incorrect = DataManager.Instance.QuesDataMgr.QuestionList.Count - m_RightQuestion;
  108. HttpSocket.Instance.SendIpTOQuestion(questionInfor);
  109. }
  110. //等待2秒后,答题面板消失,太阳系出现
  111. StartCoroutine(WaiteTime());
  112. }
  113. }
  114. IEnumerator WaiteTime()
  115. {
  116. yield return new WaitForSeconds(2f);
  117. for (int i = 0; i < m_Questions.Count; i++)
  118. {
  119. m_Questions[i].gameObject.SetActive(false);
  120. }
  121. m_P2Galax.SetActive(true);
  122. m_SucessPanel.SetActive(false);
  123. StopCoroutine(WaiteTime());
  124. }
  125. /// <summary>
  126. /// 改变交互方式
  127. /// </summary>
  128. /// <param name="type"></param>
  129. private void ChangeInputDeviceType(InputDeviceType type)
  130. {
  131. var list = API_Module_InputSystem.GetInstance().inputDeviceSupportList;
  132. for (int i = 0; i < list.Length; i++)
  133. {
  134. if (type == list[i].inputDeviceType)
  135. {
  136. API_Module_InputSystem.GetInstance().SetActiveInputDevice(list[i].inputDeviceType, true);
  137. }
  138. else
  139. {
  140. API_Module_InputSystem.GetInstance().SetActiveInputDevice(list[i].inputDeviceType, false);
  141. }
  142. }
  143. }
  144. private void OnDisable()
  145. {
  146. m_RightQuestion = 0;
  147. m_CurrentScore = 0;
  148. m_AlreadyQuestion.Clear();
  149. for (int i = 0; i < m_Questions.Count; i++)
  150. {
  151. m_Questions[i].gameObject.SetActive(true);
  152. }
  153. m_P2Galax.SetActive(false);
  154. m_SucessPanel.SetActive(false);
  155. // ChangeInputDeviceType(InputDeviceType.GGT26Dof);
  156. }
  157. }