using UnityEngine; using System.Collections.Generic; namespace SK.Framework { /// /// 问题处理器 /// public class QuestionsHandler { //问题列表 private List questions; //当前题号 private int currentSequence; /// /// 构造函数 /// /// 问题配置文件 public QuestionsHandler(QuestionsProfile profile) { Init(profile); } /// /// 构造函数 /// /// 配置文件的路径 public QuestionsHandler(string resourcesPath) { QuestionsProfile profile = Resources.Load(resourcesPath); if (profile == null) { Debug.LogError(string.Format("加载问题配置文件失败 {0}", resourcesPath)); } else { Init(profile); } } //初始化 private void Init(QuestionsProfile profile) { questions = new List(); for (int i = 0; i < profile.Judges.Count; i++) { questions.Add(profile.Judges[i]); } for (int i = 0; i < profile.SingleChoices.Count; i++) { questions.Add(profile.SingleChoices[i]); } for (int i = 0; i < profile.MultipleChoices.Count; i++) { questions.Add(profile.MultipleChoices[i]); } for (int i = 0; i < profile.Completions.Count; i++) { questions.Add(profile.Completions[i]); } for (int i = 0; i < profile.Essays.Count; i++) { questions.Add(profile.Essays[i]); } } /// /// 上一题 /// /// public QuestionBase Last() { currentSequence--; currentSequence = Mathf.Clamp(currentSequence, 1, questions.Count); return questions.Find(m => m.Sequence == currentSequence); } /// /// 下一题 /// /// public QuestionBase Next() { currentSequence++; currentSequence = Mathf.Clamp(currentSequence, 1, questions.Count); return questions.Find(m => m.Sequence == currentSequence); } /// /// 根据题号切换到指定问题 /// /// 题号 /// public QuestionBase Switch(int sequence) { currentSequence = sequence; currentSequence = Mathf.Clamp(currentSequence, 1, questions.Count); return questions.Find(m => m.Sequence == currentSequence); } /// /// 根据题号获取问题 /// /// 题型 /// 题号 /// public T Get(int sequence) where T : QuestionBase { var question = questions.Find(m => m.Sequence == sequence); return question != null ? question as T : null; } } }