当我尝试告诉IF命令检查是否等同于XML文件中的值时,IF命令不允许您这样做。
此代码是读取XML文件的代码:
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UnityEngine;
public class ReadXML : MonoBehaviour
{
// Use this for initialization
void Start()
{
List<Dictionary<string, string>> allTextDic = parseFile();
Dictionary<string, string> dic = allTextDic[0];
}
public List<Dictionary<string, string>> parseFile()
{
TextAsset txtXmlAsset = Resources.Load<TextAsset>("riddles");
var doc = XDocument.Parse(txtXmlAsset.text);
var allDict = doc.Element("document").Elements("row");
List<Dictionary<string, string>> allTextDic = new
List<Dictionary<string, string>>();
foreach (var oneDict in allDict)
{
var twoStrings = oneDict.Elements("string");
XElement element1 = twoStrings.ElementAt(0);
XElement element2 = twoStrings.ElementAt(1);
string first = element1.ToString().Replace("<string>",
"").Replace("
</string>", "");
string second = element2.ToString().Replace("<string>",
"").Replace("</string>", "");
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("1", first);
dic.Add("2", second);
allTextDic.Add(dic);
}
return allTextDic;
}
}
这是XML文件:
<document>
<row>
<string>1</string >
<string>2</string >
</row>
<row>
<string>3</string >
<string>4</string >
</row>
<row>
<string>5</string >
<string>6</string >
</row>
<row>
<string>7</string >
<string>8</string >
</row>
</document>
好的,这是我的问题。当您在Visual Studio上看到此脚本时,下面显示“ dic”的行下有一条红色的弯曲线。为什么呢? dic [“ 1st”]的值与1相同,但是API不允许这样做,那么我们该如何解决?
public GameObject obj;
private void OnMouseDown()
{
if (dic["1st"] == 1)
{
Instantiate(obj);
}
}
谢谢!
答案 0 :(得分:0)
请小心==
运算符;尝试:
public GameObject obj;
private void OnMouseDown()
{
if (Convert.ToInt32(dic["1st"]) == 1)
{
Instantiate(obj);
}
}