在Unity Inspector

时间:2016-05-17 23:35:20

标签: xml unity3d xml-parsing

我有一个XML数据库(定制),我试图在Unity3D中加载 我有一个公共的TextAsset,我在检查器中指定它等于我的XML数据库 当我运行我的脚本时,它会在行XMLDocument.loadxml()上抛出NullReferenceException。
当我简化它时,只使用XML格式的简单字符串,并将其更改为XMLDocument.Load()(具有相同的标记和数据),它可以工作。尽管如此,这远远不适合写作。

public TextAsset enemyXML;  
This one works--> //public string enemyXml = "<Enemy><EnemyName> Scale </EnemyName><EnemyID> 001 </EnemyID><EnemySpriteTag> Scale </EnemySpriteTag><EnemyBrawn>8  </EnemyBrawn><EnemyBrisk> 8 </EnemyBrisk><EnemyBrains>8</EnemyBrains>   <EnemyBravery> 10 </EnemyBravery><EnemyAbilitySet1> Scale </EnemyAbilitySet1>   <EnemyAbilitySet2> None </EnemyAbilitySet2><EnemyAbilitySet3> None    </EnemyAbilitySet3> <EnemyAbilitySet4> None </EnemyAbilitySet4></Enemy>"; 

XmlDocument xmlDoc = new XmlDocument();
This one works--> //xmlDoc.Load(new StringReader(enemyXml));
xmlDoc.LoadXml(enemyXml.text);
XmlNodeList enemyList = xmlDoc.GetElementsByTagName("Enemy");  
foreach (XmlNode enemyInfo in enemyList)
        {
        XmlNodeList EnemyContent = enemyInfo.ChildNodes;
        enemyDictionary = new Dictionary<string, string>(); //in Items tag, first tag, text associated with
        foreach (XmlNode content in EnemyContent)
        {
            switch(content.Name)//must contain all things for each item
            {
                case "EnemyName":
                    enemyDictionary.Add("EnemyName", content.InnerText);
                    break;
                case "EnemyID":
                    enemyDictionary.Add("EnemyID", content.InnerText);
                    break;
                case "EnemySpriteTag":
                    enemyDictionary.Add("EnemySpriteTag", content.InnerText);
                    break;
                case "EnemyBrawn":
                    enemyDictionary.Add("EnemyBrawn", content.InnerText);
                    break;
                case "EnemyBrisk":
                    enemyDictionary.Add("EnemyBrisk", content.InnerText);
                    break;
                case "EnemyBrains":
                    enemyDictionary.Add("EnemyBrains", content.InnerText);
                    break;
                case "EnemyBravery":
                    enemyDictionary.Add("EnemyBravery", content.InnerText);
                    break;
                case "EnemyAbilitySet1":
                    enemyDictionary.Add("EnemyAbilitySet1", content.InnerText);
                    break;
                case "EnemyAbilitySet2":
                    enemyDictionary.Add("EnemyAbilitySet2", content.InnerText);
                    break;
                case "EnemyAbilitySet3":
                    enemyDictionary.Add("EnemyAbilitySet3", content.InnerText);
                    break;
                case "EnemyAbilitySet4":
                    enemyDictionary.Add("EnemyAbilitySet4", content.InnerText);
                    break;
            }
        }
        EnemyDictionList.Add(enemyDictionary);
    }  

这就是我能做到的所有相关代码。 自定义XML文件被包装,上面的字符串被推入中间。 我只是错过了拼写,还是什么?我现在已经迷失了几天,没有找到运气。

如果在编辑中需要,我可以包括BaseEnemy类。

编辑: 这也适用于我的其余代码。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Path.Combine(Application.dataPath,"RPGEnemyDatabase.xml"));

尽管如此,试图在检查器中分配它,或试图说出enemyXML.text,但是没有。

2 个答案:

答案 0 :(得分:0)

试试这个

using System.IO;

...

XmlDocument xmlDoc = new XmlDocument();

string xmlContent = File.ReadAllText(enemyXML);

xmlDoc.LoadXML(enemyXML);

编辑:
您可能希望在此处查看我的解决方案:Unity C# How to set text as XML component?

答案 1 :(得分:0)

XMLFILE

<EnemyCollection>
   <Enemies>
      <Enemy>
         <EnemyName>Scale</EnemyName>
         <EnemyID>001</EnemyID>
         <EnemySpriteTag>Scale</EnemySpriteTag>
         <EnemyBrawn>8</EnemyBrawn>
         <EnemyBrisk>8</EnemyBrisk>
         <EnemyBrains>8</EnemyBrains>   
         <EnemyBravery>10</EnemyBravery>
         <EnemyAbilitySet1>Scale</EnemyAbilitySet1>   
         <EnemyAbilitySet2>None</EnemyAbilitySet2>
         <EnemyAbilitySet3>None</EnemyAbilitySet3> 
         <EnemyAbilitySet4>None</EnemyAbilitySet4>
      </Enemy>
   </Enemies>
</EnemyCollection>

使用

创建另一个类文件
public class Enemy {

    public string EnemyName { get; set; }
    public string EnemyID { get; set; }
    public string EnemySpriteTag { get; set; }
    public string EnemyBrawn { get; set; }
    public string EnemyBrisk { get; set; }
    public string EnemyBrains { get; set; }
    public string EnemyBravery { get; set; }
    public string EnemyAbilitySet1 { get; set; }
    public string EnemyAbilitySet2 { get; set; }
    public string EnemyAbilitySet3 { get; set; }
    public string EnemyAbilitySet4 { get; set; }
}

[XmlRoot("EnemyCollection")]
public class EnemiesContainer
{
    [XmlArray("Enemies"), XmlArrayItem("Enemy")]
    public Enemy[] enemies;

    public static EnemiesContainer LoadFromText(string text)
    {
       var serializer = new XmlSerializer(typeof(EnemiesContainer));
       return serializer.Deserialize(new StringReader(text)) as EnemiesContainer;
    }

}

最后在你检索xml数据的主类中,你仍然会从TextAsset中读取xml

public TextAsset xml;

void Start () {

    string xmlContent = xml.text;
    var enemyData = EnemiesContainer.LoadFromText(xmlContent);


    foreach (var enemy in enemyData.enemies)
    {
        Debug.Log(enemy.EnemyName);
        Debug.Log(enemy.EnemyID);
        Debug.Log(enemy.EnemySpriteTag);
        Debug.Log(enemy.EnemyBrawn);
        Debug.Log(enemy.EnemyBrisk);
        Debug.Log(enemy.EnemyBrains);
        //...
    }

}

供进一步参考 http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer