1. 程式人生 > >unity xml的讀取和寫入和創建

unity xml的讀取和寫入和創建

功能 edit eno sum load 定義 鏈接庫 cto tab

unity xml的讀取和寫入

Xml是一種常用的數據格式,方便數據的索引查找

1.首先引入相關的動態鏈接庫:

1.1System.Data.dll

1.2Excel.DLL

1.3文件應用擡頭

using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using Excel;

2.根據excel生成對應的xml(寫xml)

2.1:excel轉為常用的dataset格式

/// <summary>
    /// 根據Excel的path生成對應的dataset
    /// </summary>
    /// <param name="path"></Excel地址:常為Application.dataPath+ "/StreamingAssets/" + "Excel文件名.xlsx">
    /// <returns></returns>
    public DataSet getDataset(string path)
    {
        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader 
= ExcelReaderFactory.CreateOpenXmlReader(fs); DataSet ds = excelReader.AsDataSet(); fs.Dispose(); return ds; }

2.2:根據2.1生成的dataset中的table參考自定義方式生成對應的xml

  /// <summary>
  /// 將dataset數據根據字符串索引進行xml序列化
  /// </summary>
  /// <param name="xmlname"></生成的xml名字>
/// <param name="DT"></dataset的table> /// <param name="strAtt"></父節點序列> /// <param name="strelement"></子節點序列> public void Toxml(string xmlname,DataTable DT,string strAtt,string[] strelement) { if (DT != null) { if (DT.Rows.Count > 0) { if (!Directory.Exists(Application.streamingAssetsPath)) { Directory.CreateDirectory(Application.streamingAssetsPath); } string path = Application.streamingAssetsPath + "/" + xmlname + ".xml"; if (File.Exists(path)) { File.Delete(path); } XmlDocument writer = new XmlDocument(); XmlElement x100 = writer.CreateElement(strAtt); for (int i = 0; i < DT.Rows.Count; i++) { XmlElement x10 = writer.CreateElement(strelement[0]); for (int j = 0; j < strelement.Length-1; j++) { XmlAttribute xa = writer.CreateAttribute(strelement[j]); xa.Value = DT.Rows[i][j].ToString(); x10.Attributes.Append(xa); } x100.AppendChild(x10); } writer.AppendChild(x100); writer.Save(path); } } }

3.常見獲取xml中的數據(讀xml)

  public string GetTextNameByGameObjectName(string TargetParentName,string gameobjName)
    {
        testname = null;
      
        XmlNodeList xmlNodeList = doc.SelectSingleNode("test").ChildNodes;

        foreach (XmlElement tempnode in xmlNodeList)
        {
            if (TargetParentName == tempnode.GetAttribute("parentTargetname") && gameobjName == tempnode.GetAttribute("scenesname"))
            {
                testname = tempnode.GetAttribute("textname");

            }
            else
            {
              //  Debug.LogError("table didnt have the Attribute");
            }         
            }
        return testname;   
    }

5.寫入xml

  public void WriteToxml(string NodeAttritube,string InnerTex)
    {
        _XmlPath = Application.dataPath + "/StreamingAssets/" + "che.xml";
        LoadXmlFileByPath(_XmlPath);
        XmlNodeList xmlNodeList = doc.SelectSingleNode("合成工具列表").ChildNodes;
        foreach (XmlElement tempNode in xmlNodeList)
        {

            if (NodeAttritube == tempNode.GetAttribute("Tool4ID"))
            {

                tempNode.SetAttribute("Tool4ID", InnerTex);
            }

          
        }    
        doc.Save(_XmlPath);
          
    }

4.增加編輯器功能,方便策劃改表後自行生成xml

在類中增加方法 
public void CreatXml()
    {
        _Excelpath= Application.dataPath + "/StreamingAssets/" + "ExplosionList.xlsx";
        _XmlPath = Application.dataPath + "/StreamingAssets/" + "che.xml";
         ds =  getDataset(_Excelpath);
         Toxml("che", ds.Tables[0], "合成工具列表", newstress0);
       
    }


在另外的編輯器類寫入
public class CompositeXmlMake : Editor {
    [MenuItem("Tools/CreatCompositeXml")]

    public static void CreatXML()
    {
        ConpositeXml.GetInstance().CreatXml();
    }

unity xml的讀取和寫入和創建