123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class Question : MonoBehaviour
- {
- public event Action<int, bool, Question> CalScoreQuestion;
- private GameObject m_ItemPrefab = null;
- private Text m_Question;
- private string m_QuestionText;
- private Text m_Title;
- private int m_RightID;
- private List<string> m_AnswerData = new List<string>();
- private List<QuestionItemToggle> m_AnswerList;
- private QuestionItemToggle m_CurrentAns;
- private int m_Score = 0;
- public List<string> AnswerData
- {
- get { return m_AnswerData; }
- set
- {
- if (m_AnswerData.Count > 0)
- {
- m_AnswerData = null;
- }
- m_AnswerData = value;
- }
- }
- public int RightID
- {
- get { return m_RightID; }
- set { m_RightID = value; }
- }
- public string QuestionText
- {
- get { return m_QuestionText; }
- set
- {
- if (value != null)
- {
- m_QuestionText = value;
- m_Question.text = m_QuestionText;
- }
- }
- }
- public int Score
- {
- get { return m_Score; }
- set { m_Score = value; }
- }
- void Awake()
- {
- m_CurrentAns = null;
- m_AnswerList = new List<QuestionItemToggle>();
- m_ItemPrefab = transform.Find("Canvas/Question/Panel/QuestionItem").gameObject;
- m_Question = transform.Find("Canvas/Question").GetComponent<Text>();
- m_Title = transform.Find("Canvas/Title").GetComponent<Text>();
- }
- private void Start()
- {
- //根据数据生成UI面板,当有数据后可使用
- for (int i = 0; i < AnswerData.Count; i++)
- {
- var item = GetQuestionItem();
- item.OnValueChange += OnClickAnswer;
- item.ID = i;
- item.AnswerText = AnswerData[i];
- item.Init();
- m_AnswerList.Add(item);
- }
- }
- private void OnClickAnswer(QuestionItemToggle item, bool value)
- {
- var isanswer = m_RightID == item.ID ? true : false;
- item.JudgeAnswer(isanswer);
- if (m_CurrentAns != null)
- {
- m_CurrentAns.ChangeState(value);
- }
- m_CurrentAns = item;
- if (value)
- {
- m_Title.gameObject.SetActive(true);
- m_Title.text = isanswer == true ? "回答正确" : "回答错误再试一次";
- }
- else
- {
- m_Title.gameObject.SetActive(false);
- }
- //计算玩家答对几道题
- if (null != CalScoreQuestion)
- {
- CalScoreQuestion(m_Score, isanswer, this);
- }
- SetToggleInteractable();
- }
- private QuestionItemToggle GetQuestionItem()
- {
- QuestionItemToggle item = null;
- var parenttra = m_Question.transform.Find("Panel");
- var obj = Instantiate(m_ItemPrefab, parenttra);
- obj.transform.localRotation = Quaternion.identity;
- obj.transform.localScale = new Vector3(1, 1, 1);
- obj.SetActive(true);
- item = obj.GetComponent<QuestionItemToggle>();
- return item;
- }
- /// <summary>
- /// 调用方法,将点击的结果返回到服务器
- /// </summary>
- private void SendAnswerMessage(string msg)
- {
- Debug.Log(msg);
- }
- private void SetToggleInteractable()
- {
- for (int i = 0; i < m_AnswerList.Count; i++)
- {
- m_AnswerList[i].SetInteractable(false);
- }
- }
- private void OnDisable()
- {
- for (int i = 0; i < m_AnswerList.Count; i++)
- {
- m_AnswerList[i].SetInteractable(true);
- }
- m_Title.gameObject.SetActive(false);
- }
- }
|