private SelectTopic random(SelectTopic topic) { int resultIndex = 0; for (int i = 0; i < topic.options.Count; i++) { if (topic.options[i].value == topic.result) { resultIndex = i; } } for (int i = topic.options.Count - 1; i >= 0; i--) { int index = UnityEngine.Random.Range(0, i + 1); OptionType newType = topic.options[i].value; topic.options[i].value = topic.options[index].value; topic.options[index].value = newType; } topic.result = topic.options[resultIndex].value; return(topic); }
/// <summary> /// 初始化选项界面 /// </summary> /// <param name="options"></param> public void showSelectTopicUI(int selectIndex, Action <SelectTopic, Option> action) { this.currentToggle = null; this.action = action; gameObject.SetActive(true); gameObject.transform.localPosition = Vector3.zero; this.currentTopic = selectTopicDic[selectIndex]; TopicLabel.text = currentTopic.content; currentTopic = random(currentTopic); foreach (Option option in currentTopic.options) { switch (option.value) { case OptionType.A: ALabel.text = option.text; NGUITools.FindInParents <UIToggle>(ALabel.gameObject).optionCanBeNone = true; NGUITools.FindInParents <UIToggle>(ALabel.gameObject).value = false; break; case OptionType.B: BLabel.text = option.text; NGUITools.FindInParents <UIToggle>(BLabel.gameObject).optionCanBeNone = true; NGUITools.FindInParents <UIToggle>(BLabel.gameObject).value = false; break; case OptionType.C: CLabel.text = option.text; NGUITools.FindInParents <UIToggle>(CLabel.gameObject).optionCanBeNone = true; NGUITools.FindInParents <UIToggle>(CLabel.gameObject).value = false; break; case OptionType.D: DLabel.text = option.text; NGUITools.FindInParents <UIToggle>(CLabel.gameObject).optionCanBeNone = true; NGUITools.FindInParents <UIToggle>(CLabel.gameObject).value = false; break; } } }
private void readConfig(string xmlPath) { this.selectTopicDic = new Dictionary <int, SelectTopic>(); XmlUtil xmlUtil = new XmlUtil(xmlPath, true); XmlNode nodeDatas = xmlUtil.GetChildXPathNode("data")[0]; foreach (XmlNode topicNode in xmlUtil.GetChildNodesByName(nodeDatas, "题目")) { SelectTopic topic = new SelectTopic(); topic.index = Int32.Parse(xmlUtil.ReadAttrValue(topicNode, "编号")); topic.content = xmlUtil.ReadAttrValue(topicNode, "题目内容"); topic.result = (OptionType)Enum.Parse(typeof(OptionType), xmlUtil.ReadAttrValue(topicNode, "答案")); topic.options = new List <Option>(); foreach (XmlNode nodeOption in xmlUtil.GetChildNodesByName(topicNode, "选项")) { Option option = new Option(); option.value = (OptionType)Enum.Parse(typeof(OptionType), xmlUtil.ReadAttrValue(nodeOption, "编号")); option.text = xmlUtil.ReadAttrValue(nodeOption, "选项内容"); topic.options.Add(option); } this.selectTopicDic.Add(topic.index, topic); } }