using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameRoot : MonoBehaviour { public static GameRoot Instance = null; //记录答案字典 public Dictionary> _answerDict = new Dictionary>(); private void Awake() { DontDestroyOnLoad(this); Instance = this; } // Start is called before the first frame update void Start() { SubmitAnswer(1,1,1); SubmitAnswer(1, 2, 2); SubmitAnswer(2, 1, 2); CheckAnswer(); } // Update is called once per frame void Update() { } /// /// 公共提交答案函数 /// PassIndex:关卡索引 /// QuestionIndex:问题索引 /// answer:记录用户所选的答案(ABCD对应0123) /// /// 关卡索引 /// 问题索引 /// 记录用户所选的答案(ABCD对应0123) public static void SubmitAnswer(int PassIndex,int QuestionIndex,int answer) { if (GameRoot.Instance == null) return; //如果不存在则创建并放入字典 if (!GameRoot.Instance._answerDict.ContainsKey(PassIndex)) { Dictionary dict = new Dictionary(); dict.Add(QuestionIndex, answer); GameRoot.Instance._answerDict.Add(PassIndex, dict); Debug.Log("记录第" + PassIndex + "关卡" + QuestionIndex + "的问题答案为" + answer); } else { //继续记录 Dictionary dict = new Dictionary(); if (GameRoot.Instance._answerDict.TryGetValue(PassIndex, out dict)) { dict.Add(QuestionIndex, answer); GameRoot.Instance._answerDict[PassIndex] = dict; Debug.Log("关卡字典已存在,更新" + QuestionIndex + "问题的答案为" + answer); } } } public void CheckAnswer() { int passCount = _answerDict.Keys.Count; Debug.Log("总关卡:"+passCount); for (int i = 0; i < passCount+1; i++) { Dictionary dict = new Dictionary(); if(_answerDict.TryGetValue(i, out dict)) { int questionCount = dict.Keys.Count; // Debug.Log("第" + i + "关有" + questionCount + "道题"); for (int j = 0; j < questionCount+1; j++) { if (dict.ContainsKey(j)) { Debug.Log("第"+i+"关第" + j + "题用户作答为" + dict[j]); } } } } } }