123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- using SC.XR.Unity.Module_InputSystem;
- /*
- * 答题模块的控制面板
- */
- public class QuestionControl : MonoBehaviour
- {
- private GameObject m_Prefab;
- private GameObject m_SucessPanel;
- private Text m_SucessText;
- private int m_CurrentScore;
- private int m_RightQuestion;
- //已经回答的题目
- private List<Question> m_AlreadyQuestion;
- //所有题目
- private List<Question> m_Questions;
- /// <summary>
- /// 是否向服务器发送数据
- /// </summary>
- [SerializeField]
- private bool IsSendMsgToServer;
- ///// <summary>
- ///// 等待太阳系现时间
- ///// </summary>
- //[SerializeField]
- //private float m_WaitP2GalaxTime;
- //太阳系
- public GameObject m_P2Galax;
- private void OnEnable()
- {
- //ChangeInputDeviceType(InputDeviceType.Head);
- }
- private void Awake()
- {
- DataManager.Instance.Init();
- }
- private void Start()
- {
- m_CurrentScore = 0;
- m_RightQuestion = 0;
- m_SucessPanel = transform.Find("Sucess").gameObject;
- m_SucessText = m_SucessPanel.transform.Find("Text").GetComponent<Text>();
- m_Prefab = transform.Find("Question").gameObject;
- m_Prefab.SetActive(false);
- m_AlreadyQuestion = new List<Question>();
- m_Questions = new List<Question>();
- CraetQuestions();
- }
- public void Update()
- {
- }
- /// <summary>
- /// 根据服务器数据创建问题UI面板
- /// </summary>
- private void CraetQuestions()
- {
- var data = DataManager.Instance.QuesDataMgr.QuestionList;
- for (int i = 0; i < data.Count; i++)
- {
- var obj = Instantiate(m_Prefab) as GameObject;
- obj.SetActive(true);
- var question = obj.AddComponent<Question>();
- question.transform.SetParent(this.transform);
- question.transform.localPosition = data[i].position;
- question.transform.localRotation = Quaternion.Euler(data[i].rotation);
- question.transform.localScale = data[i].scale;
- question.QuestionText = data[i].question;
- question.AnswerData.AddRange(data[i].Answer);
- question.RightID = data[i].RightAnswer;
- question.Score = data[i].score;
- question.CalScoreQuestion += CalScore;
- m_Questions.Add(question);
- }
- }
- /// <summary>
- /// 计算玩家获得的分数
- /// </summary>
- /// <param name="score"></param>
- /// <param name="isright"></param>
- /// <param name="question"></param>
- private void CalScore(int score, bool isright, Question question)
- {
- if (!m_AlreadyQuestion.Contains(question))
- {
- m_AlreadyQuestion.Add(question);
- if (isright)
- {
- m_CurrentScore += score;
- m_RightQuestion += 1;
- }
- }
- if (m_AlreadyQuestion.Count >= DataManager.Instance.QuesDataMgr.QuestionList.Count)
- {
- m_SucessPanel.SetActive(true);
- string str = String.Format("{0}{1}{2}{3}{4}", "恭喜你答对", m_RightQuestion.ToString(), "道题,获得", m_CurrentScore.ToString(), "积分");
- m_SucessText.text = str;
- //发送信息到服务器
- if (IsSendMsgToServer)
- {
- SendQuestionInfor questionInfor = new SendQuestionInfor();
- questionInfor.sn = DeviceSN.Instance.SendSerialBy16();
- questionInfor.correct = m_RightQuestion;
- questionInfor.incorrect = DataManager.Instance.QuesDataMgr.QuestionList.Count - m_RightQuestion;
- HttpSocket.Instance.SendIpTOQuestion(questionInfor);
- }
- //等待2秒后,答题面板消失,太阳系出现
- StartCoroutine(WaiteTime());
- }
- }
- IEnumerator WaiteTime()
- {
- yield return new WaitForSeconds(2f);
- for (int i = 0; i < m_Questions.Count; i++)
- {
- m_Questions[i].gameObject.SetActive(false);
- }
- m_P2Galax.SetActive(true);
- m_SucessPanel.SetActive(false);
- StopCoroutine(WaiteTime());
- }
- /// <summary>
- /// 改变交互方式
- /// </summary>
- /// <param name="type"></param>
- private void ChangeInputDeviceType(InputDeviceType type)
- {
- var list = API_Module_InputSystem.GetInstance().inputDeviceSupportList;
- for (int i = 0; i < list.Length; i++)
- {
- if (type == list[i].inputDeviceType)
- {
- API_Module_InputSystem.GetInstance().SetActiveInputDevice(list[i].inputDeviceType, true);
- }
- else
- {
- API_Module_InputSystem.GetInstance().SetActiveInputDevice(list[i].inputDeviceType, false);
- }
- }
- }
- private void OnDisable()
- {
- m_RightQuestion = 0;
- m_CurrentScore = 0;
- m_AlreadyQuestion.Clear();
- for (int i = 0; i < m_Questions.Count; i++)
- {
- m_Questions[i].gameObject.SetActive(true);
- }
- m_P2Galax.SetActive(false);
- m_SucessPanel.SetActive(false);
- // ChangeInputDeviceType(InputDeviceType.GGT26Dof);
- }
- }
|