数据生成XML导入Excel

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class EffectPointList : EditorWindow {

public GameObject object1;

public static EffectPointList listWindow;
private string searchFolder = "Assets/Art/Characters";
private string[] prefabPathList;
private List<GameObject> instantiatePrefab = new List<GameObject>();
Dictionary<string, PrefabDict> prefabDict = new Dictionary<string, PrefabDict>();

[MenuItem("Assets/ListEffectPoints")]

static void Start()
{
listWindow = (EffectPointList)EditorWindow.GetWindow(typeof(EffectPointList), true, "特效点列表");
listWindow.minSize = new Vector2(400, 200);
listWindow.Show();
}

void OnGUI()
{
EditorGUILayout.HelpBox("在Assets/Art/Characters/Editor下生成EffectPointsInfo.xml。", MessageType.Info);

if (GUILayout.Button("生成特效点信息", GUILayout.Height(40)))
{
instantiatePrefab.Clear();
GetAllPrefab();

Dictionary<string, PrefabDict> xmlDict = CheckAllPrefab();

GenerateXML(xmlDict);

}
}

void GetAllPrefab()
{
prefabPathList = AssetDatabase.FindAssets("t:Prefab", new string[] { searchFolder });

for (int i = 0; i < prefabPathList.Length; ++i)
{
string prefabPath = AssetDatabase.GUIDToAssetPath(prefabPathList[i]);
GameObject obj = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

if (!prefabPath.Contains("Effects"))
{
GameObject clone = (GameObject)PrefabUtility.InstantiatePrefab(obj);
instantiatePrefab.Add(clone);
}
}
}

Dictionary<string, PrefabDict> CheckAllPrefab()
{
prefabDict.Clear();

foreach (var obj in instantiatePrefab)
{
if (obj == null)
continue;

PrefabDict pfb = new PrefabDict();

foreach (Transform child in obj.GetComponentsInChildren<Transform>() )
{
if( child.name.Contains("Head_C_EffectPoint") )
pfb.Head_C_EffectPoint = true;
if( child.name.Contains("Head_T_EffectPoint") )
pfb.Head_T_EffectPoint = true;
if( child.name.Contains("Hand_L_EffectPoint") )
pfb.Hand_L_EffectPoint = true;
if( child.name.Contains("Hand_R_EffectPoint") )
pfb.Hand_R_EffectPoint = true;

if( child.name.Contains("Foot_L_EffectPoint") )
pfb.Foot_L_EffectPoint = true;
if( child.name.Contains("Foot_R_EffectPoint") )
pfb.Foot_R_EffectPoint = true;
if( child.name.Contains("Chest_EffectPoint") )
pfb.Chest_EffectPoint = true;
if( child.name.Contains("Bottom_EffectPoint") )
pfb.Bottom_EffectPoint = true;

if( child.name.Contains("Weapon01_EffectPoint") )
pfb.Weapon01_EffectPoint = true;
if( child.name.Contains("Normal_HurtPoint") )
pfb.Normal_HurtPoint = true;
if( child.name.Contains("Special_HurtPoint") )
pfb.Special_HurtPoint = true;
if( child.name.Contains("Footsteps_EffectPoint") )
pfb.Footsteps_EffectPoint = true;

}

prefabDict.Add(obj.name, pfb);

DestroyImmediate(obj);
}

return prefabDict;
}

private class PrefabDict
{
public bool Head_C_EffectPoint = false;
public bool Head_T_EffectPoint = false;
public bool Hand_L_EffectPoint = false;
public bool Hand_R_EffectPoint = false;

public bool Foot_L_EffectPoint = false;
public bool Foot_R_EffectPoint = false;
public bool Chest_EffectPoint = false;
public bool Bottom_EffectPoint = false;

public bool Weapon01_EffectPoint = false;
public bool Normal_HurtPoint = false;
public bool Special_HurtPoint = false;
public bool Footsteps_EffectPoint = false;
}

void GenerateXML(Dictionary<string, PrefabDict> dict)
{
string xmlPath = Application.dataPath + "/Editor/EffectPointsInfo.xml";
if (File.Exists(xmlPath))
{
Debug.Log(xmlPath);
File.Delete(xmlPath);
}

XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = xmlDoc.DocumentElement;
xmlDoc.InsertBefore(xmldecl, root);

XmlElement infoBase = xmlDoc.CreateElement("InfoBase"); //创建根节点,最上层节点 角色名
xmlDoc.AppendChild(infoBase);

foreach (var dic in dict)
{
XmlElement characterInfo = xmlDoc.CreateElement("effectPointsInfo");
infoBase.AppendChild(characterInfo);

XmlElement characterKey = xmlDoc.CreateElement("Character");

XmlElement Head_C_EffectPoint = xmlDoc.CreateElement("Head_C_EffectPoint");
XmlElement Head_T_EffectPoint = xmlDoc.CreateElement("Head_T_EffectPoint");
XmlElement Hand_L_EffectPoint = xmlDoc.CreateElement("Hand_L_EffectPoint");
XmlElement Hand_R_EffectPoint = xmlDoc.CreateElement("Hand_R_EffectPoint");

XmlElement Foot_L_EffectPoint = xmlDoc.CreateElement("Foot_L_EffectPoint");
XmlElement Foot_R_EffectPoint = xmlDoc.CreateElement("Foot_R_EffectPoint");
XmlElement Chest_EffectPoint = xmlDoc.CreateElement("Chest_EffectPoint");
XmlElement Bottom_EffectPoint = xmlDoc.CreateElement("Bottom_EffectPoint");

XmlElement Weapon01_EffectPoint = xmlDoc.CreateElement("Weapon01_EffectPoint");
XmlElement Normal_HurtPoint = xmlDoc.CreateElement("Normal_HurtPoint");
XmlElement Special_HurtPoint = xmlDoc.CreateElement("Special_HurtPoint");
XmlElement Footsteps_EffectPoint = xmlDoc.CreateElement("Footsteps_EffectPoint");

characterKey.InnerText = dic.Key.ToString();

Head_C_EffectPoint.InnerText = dic.Value.Head_C_EffectPoint.ToString();
Head_T_EffectPoint.InnerText = dic.Value.Head_T_EffectPoint.ToString();
Hand_L_EffectPoint.InnerText = dic.Value.Hand_L_EffectPoint.ToString();
Hand_R_EffectPoint.InnerText = dic.Value.Hand_R_EffectPoint.ToString();

Foot_L_EffectPoint.InnerText = dic.Value.Foot_L_EffectPoint.ToString();
Foot_R_EffectPoint.InnerText = dic.Value.Foot_R_EffectPoint.ToString();
Chest_EffectPoint.InnerText = dic.Value.Chest_EffectPoint.ToString();
Bottom_EffectPoint.InnerText = dic.Value.Bottom_EffectPoint.ToString();

Weapon01_EffectPoint.InnerText = dic.Value.Weapon01_EffectPoint.ToString();
Normal_HurtPoint.InnerText = dic.Value.Normal_HurtPoint.ToString();
Special_HurtPoint.InnerText = dic.Value.Special_HurtPoint.ToString();
Footsteps_EffectPoint.InnerText = dic.Value.Footsteps_EffectPoint.ToString();

characterInfo.AppendChild(characterKey);
characterInfo.AppendChild(Head_C_EffectPoint);
characterInfo.AppendChild(Head_T_EffectPoint);
characterInfo.AppendChild(Hand_L_EffectPoint);
characterInfo.AppendChild(Hand_R_EffectPoint);

characterInfo.AppendChild(Foot_L_EffectPoint);
characterInfo.AppendChild(Foot_R_EffectPoint);
characterInfo.AppendChild(Chest_EffectPoint);
characterInfo.AppendChild(Bottom_EffectPoint);

characterInfo.AppendChild(Weapon01_EffectPoint);
characterInfo.AppendChild(Normal_HurtPoint);
characterInfo.AppendChild(Special_HurtPoint);
characterInfo.AppendChild(Footsteps_EffectPoint);

}

xmlDoc.Save(xmlPath); //创建EffectPointsInfo.xml文件
AssetDatabase.Refresh();
this.ShowNotification( new GUIContent("数据生成!") );
}
}