两种不同文件的特点
文件类型 | 优点 | 缺点 |
---|---|---|
XML | 易于修改,可以在任意文本编辑器中打开 | 读取速度慢,只能读取文本信息 |
asset | 读取速度快,支持各种Unity数据的序列化 | 数据量大的时候难以编辑,且只能在Unity里面修改 |
asset生成
这里用多语言配置文件strings.asset作为范例
Strings.cs
using System;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu()]//使用这个可以快速创建asset文件
public class Strings : ScriptableObject
{
public List<Strings.Sheet> sheets = new List<Strings.Sheet>();//用列表储存序列化数据
[Serializable]
public class Sheet
{
public string name = string.Empty;//表名,用于进行分类
public List<Strings.Param> list = new List<Strings.Param>();//ID与多语言具体文字列表
}
[Serializable]
public class Param
{
public string ID;
public string Kor;
public string Jpn;
public string Eng;
public string Ger;
public string Rus;
public string zhCN;
public string zhTW;
public string Frc;
}
}
代码写好后,在任意目录下点击右键,Creat——Strings,就可以创建一个Strings.asset文件,我们可以向里面填写一些基本的数据,以便测试。
asset文件与XML文件转换
在编辑器模式下即可完成该操作,可以创建一个编辑器窗口便于使用。
XML2AssetEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
public class XML2AssetEditor : EditorWindow
{
[MenuItem("Tools/XML2Asset")]
static void OpenExcelEditorWindow()
{
EditorWindow.GetWindow<XML2AssetEditor>();
}
/// <summary>
/// 编辑器模式下拖入asset文件即可序列化
/// </summary>
public Strings _string;
private static Dictionary<string, List<Strings.Param>> staticSheet = new Dictionary<string, List<Strings.Param>>();
/// <summary>
/// asset文件保存位置
/// </summary>
public string assetsOutputPath = "Assets/Strings.asset";
/// <summary>
/// XML文件保存位置
/// </summary>
public string xmlReadPath = "Assets/XMLNAssetFiles/XmlStrings.xml";
void OnEnable()
{
setAssetsFile();
}
private void setAssetsFile()
{
staticSheet.Clear();
foreach (Strings.Sheet item in _string.sheets)
{
staticSheet.Add(item.name, item.list);
}
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 0, 200, 50));
GUILayout.Label("将String.asset导出成XML文件");
if (GUILayout.Button("导出"))
{
WriteXML();
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 50, 200, 50));
GUILayout.Label("打开导出的XML文件所在文件夹");
if (GUILayout.Button("打开"))
{
Application.OpenURL(Application.dataPath + "/XMLNAssetFiles/");
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0,100, 300, 50));
GUILayout.Label("修改完XML文件后,保存成string.asset");
if (GUILayout.Button("保存"))
{
ReadXMLToAssets();
}
GUILayout.EndArea();
}
/// <summary>
/// 读取XML;
/// </summary>
public void ReadXMLToAssets()
{
Strings stringsInstance = ScriptableObject.CreateInstance<Strings>();
XmlDocument xDoc = new XmlDocument();
xDoc.Load(this.xmlReadPath);
XmlNode root = xDoc.SelectSingleNode("StringsList");
for (int i = 0; i < root.ChildNodes.Count; i++)
{
string tableName = root.ChildNodes[i].Attributes["Type"].Value;
Strings.Sheet rawSheet = new Strings.Sheet();
rawSheet.name = tableName;
XmlNodeList IDList = root.ChildNodes[i].ChildNodes;
for (int j = 0; j < IDList.Count; j++)
{
XmlElement key = xDoc.GetElementById("");
Strings.Param para = new Strings.Param();
para.ID = IDList[j].Attributes["ID"].Value;
para.Kor = IDList[j].ChildNodes[0].Attributes["Kor"].Value;
para.Jpn = IDList[j].ChildNodes[1].Attributes["Jpn"].Value;
para.Eng = IDList[j].ChildNodes[2].Attributes["Eng"].Value;
para.Ger = IDList[j].ChildNodes[3].Attributes["Ger"].Value;
para.Rus = IDList[j].ChildNodes[4].Attributes["Rus"].Value;
para.zhCN = IDList[j].ChildNodes[5].Attributes["zhCN"].Value;
para.zhTW = IDList[j].ChildNodes[6].Attributes["zhTW"].Value;
para.Frc = IDList[j].ChildNodes[7].Attributes["Frc"].Value;
rawSheet.list.Add(para);
}
stringsInstance.sheets.Add(rawSheet);
}
AssetDatabase.CreateAsset(stringsInstance, assetsOutputPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
/// <summary>
/// 写入XML;
/// </summary>
public void WriteXML()
{
if (staticSheet.Count == 0)
{
UnityEngine.Debug.Log("+++ staticSheet.Count== 0" + staticSheet.Count);
return;
}
XmlDocument xDoc = new XmlDocument();
XmlDeclaration dec = xDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
XmlElement root = xDoc.CreateElement("StringsList");
xDoc.AppendChild(dec);
xDoc.AppendChild(root);
foreach (KeyValuePair<string, List<Strings.Param>> typeNIDLocals in staticSheet)
{
XmlElement typeName = xDoc.CreateElement("Sheet");
typeName.SetAttribute("Type", typeNIDLocals.Key);
root.AppendChild(typeName);
//UnityEngine.Debug.Log("+++ Key== " + typeNIDLocals.Key);
for (int i = 0; i < typeNIDLocals.Value.Count; i++)
{
XmlElement key = xDoc.CreateElement("ID");
XmlElement KOR = xDoc.CreateElement("Kor");
XmlElement JPN = xDoc.CreateElement("Jpn");
XmlElement ENG = xDoc.CreateElement("Eng");
XmlElement GER = xDoc.CreateElement("Ger");
XmlElement RUS = xDoc.CreateElement("Rus");
XmlElement ZHCN = xDoc.CreateElement("zhCN");
XmlElement ZHTW = xDoc.CreateElement("zhTW");
XmlElement FRC = xDoc.CreateElement("Frc");
typeName.AppendChild(key);
key.AppendChild(KOR);
key.AppendChild(JPN);
key.AppendChild(ENG);
key.AppendChild(GER);
key.AppendChild(RUS);
key.AppendChild(ZHCN);
key.AppendChild(ZHTW);
key.AppendChild(FRC);
// UnityEngine.Debug.Log("+++ Value== " + typeNIDLocals.Value[i].ID);
key.SetAttribute("ID", typeNIDLocals.Value[i].ID);
KOR.SetAttribute("Kor", typeNIDLocals.Value[i].Kor);
JPN.SetAttribute("Jpn", typeNIDLocals.Value[i].Jpn);
ENG.SetAttribute("Eng", typeNIDLocals.Value[i].Eng);
GER.SetAttribute("Ger", typeNIDLocals.Value[i].Ger);
RUS.SetAttribute("Rus", typeNIDLocals.Value[i].Rus);
ZHCN.SetAttribute("zhCN", typeNIDLocals.Value[i].zhCN);
ZHTW.SetAttribute("zhTW", typeNIDLocals.Value[i].zhTW);
FRC.SetAttribute("Frc", typeNIDLocals.Value[i].Frc);
}
}
xDoc.Save(xmlReadPath);
AssetDatabase.Refresh();
}
}
如何使用
- 将生成的String.asset文件拖入脚本上序列化
- 打开自定义窗口进行操作
3.看下效果
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<StringsList>
<Sheet Type="UI">
<ID ID="UI0001">
<Kor Kor="골드" />
<Jpn Jpn="ゴールド" />
<Eng Eng="Gold" />
<Ger Ger="Gold" />
<Rus Rus="Золото" />
<zhCN zhCN="金币" />
<zhTW zhTW="金幣" />
<Frc Frc="Argent" />
</ID>
...
</Sheet>
...
</StringsList>