用XML生成如下数据
<?xml version="1.0"encoding="UTF-8"?>
<Transform name="MyCube" >
<Position>
<X>11</X>
<Y>89</Y>
<Z>20</Z>
</Position>
</Transform>
操作如下 在C#脚本中
// 创建一个文档
XmlDocument doc = new XmlDocument ();
void Start (){
CreatXML ();
//创建文档
void CreatXML (){
//创建一个声明
XmlDeclaration dec = doc.CreateXmlDeclaration ("1.0", "UTF-8", null);
//讲声明拼接到文档中
doc.AppendChild (dec);
//创建一个根元素节点
XmlNode rootNode = doc.CreateNode (XmlNodeType.Element, "Transform", null);
//对根元素节点添加属性
XmlAttribute attr = doc.CreateAttribute ("name");
attr.Value = "YourCube";
rootNode.Attributes.SetNamedItem (attr);
//拼接到xml文档中
doc.AppendChild (rootNode);
//创建一个子元素Position添加到根元素节点中并拼接进去
XmlElement pos = doc.CreateElement ("Position");
rootNode.AppendChild (pos);
XmlElement pos_X = doc.CreateElement ("x");
XmlElement pos_Y = doc.CreateElement ("y");
XmlElement pos_Z = doc.CreateElement ("z");
pos_X.InnerText = "78";
pos_Y.InnerText = "18";
pos_Z.InnerText = "38";
pos.AppendChild (pos_X);
pos.AppendChild (pos_Y);
pos.AppendChild (pos_Z);
//保存保存 路径和名字
//Application.dataPath的路径就是Assets文件件 //内写的就是表示在这个文件夹下
doc.Save (Application.dataPath + "/XML/MonoCodeXML.xml");}
解析上述XML数据如下
//首先拿到文档的位置,用XML文档实例去加载文档
doc.Load (Application.dataPath + "/XML/MonoCodeXML.xml");
//根据数据结构来分析如果获得想要的数据
//第一步拿到根元素节点
XmlElement root = doc.DocumentElement;
print (root.Name);得到的是Transform
//root.FirstChild就是这一层下面你的第一个子元素的节点
//XmlNode Pos_X = root.FirstChild.FirstChild;
//X子节点中的存储的数据内容;
//string Pos_X_string = Pos_X.InnerText;
//print (Pos_X_string);//打印78
//或者同过层级路径拿到这个节点的内容
XmlNode pox_x = root.SelectSingleNode ("/Transform/Position/x");//打印78
/////////////////////////////////////////////////////////////
用JSON实现如下数据 需要添加一个插件System.Json
{ "HeroName": "诺克萨斯之手",
"Hero_Hp": 582.24,
"Attack": 55.88,
"Skills": [{
"key": "Q",
"SkillName": "大厦四方",
"CD": "9/8/7/6",
"MP": 30 }, {
"key": "W",
"SkillName": "致残打击",
"CD": "9/8/7/6",
"Mp": 30}, {
"key": "E",
"SkillName": "大厦四方",
"CD": "9/8/7/6",
"MP": 30 },{
"key": "R",
"SkillName": "断头台",
"CD": "9/8/7/6",
"MP": 30}}]}
//创建一个大括号using System.Json;
JsonObject nuoke = new JsonObject ();
//方便后面接受技能数组
JsonArray Skillarr = new JsonArray ();
JsonObject Skill_Q = new JsonObject ();
JsonObject Skill_W = new JsonObject ();
JsonObject Skill_E = new JsonObject ();
JsonObject Skill_R = new JsonObject ();
//给每一个Json技能添加相应属性
//技能1
Skill_Q.Add ("key", "Q");
Skill_Q.Add ("Skill", "大杀四方");
Skill_Q.Add ("CD", "9/8/7/6");
Skill_Q.Add ("MP", "30");
//技能2
Skill_W.Add ("key", "W");
Skill_W.Add ("Skill", "致残打击");
Skill_W.Add ("CD", "9/8/7/6");
Skill_W.Add ("MP", "30");
//技能3
Skill_E.Add ("key", "E");
Skill_E.Add ("Skill", "无情铁手");
Skill_E.Add ("CD", "9/8/7/6");
Skill_E.Add ("MP", "30");
//技能4
Skill_R.Add ("key", "R");
Skill_R.Add ("Skill", "断头台");
Skill_R.Add ("CD", "9/8/7/6");
Skill_R.Add ("MP", "30");
//把四个技能添加给数组
Skillarr.Add (Skill_Q);
Skillarr.Add (Skill_W);
Skillarr.Add (Skill_E);
Skillarr.Add (Skill_R);
//添加Key-Value 按照顺序来
nuoke.Add ("HeroName", "诺克杀死之手");
nuoke.Add ("Hero_HP", "584.23");
nuoke.Add ("Attack", 22.88);
nuoke.Add ("Skills", Skillarr);
//扩展引用using System.IO;
//创建数据令的写入//using System.IO;
StreamWriter writer = new StreamWriter (Application.dataPath + "/JSion/MySystemJson.txt");
nuoke.Save (writer);
//自动缓冲
writer.AutoFlush = true;
//关闭
writer.Close ();
//刷新一下自动出现不用手动刷新引用编辑器 using UnityEditor;
AssetDatabase.Refresh ();
解析上述数据
void ParseJsonDate (){
//本地文件的读取
FileInfo file = new FileInfo (Application.dataPath + "/JSion/MySystemJson.txt");
//using System.Text;才能电池Encoding
StreamReader reader = new StreamReader (file.OpenRead (), Encoding.UTF8);
string str = reader.ReadToEnd ();
reader.Close ();
//释放资源
reader.Dispose ();
//开始解析
HeroInfo m_heroInfo = JsonUtility.FromJson<HeroInfo> (str);
print (m_heroinfo.Skills [3].SkillName);}
//闯将数据模型,接受解析的数据
//将来我们在需要数据的地方直接点出来
//引用using System;
[Serializable]
public class SkillKV{
public string key = "";
public string Skill = "";
public string CD = "";
public string MP = "";}
[Serializable]//修饰对象作用是将对象统一序列化为二进制的数据流的byte[]
public class HeroInfo{
public string HeroName = "";
public float Hero_HP = 0f;
public float Attack = 0f;
public List<SkillKV> Skills = new List<SkillKV> ();}
用LitJSON 写出来就是 添加一个插件LitJson
//引用using LitJson;
//创建一个JSON结构的数据
JsonData nuoks = new JsonData ();
nuoks ["HeroName"] = "诺克萨斯之手";
nuoks ["HeroHP"] = 583.55;
nuoks ["Attack"] = 100.39;
//创建一个新的内包含 随后的技能直接让["Skills"].add()
nuoks ["Skills"] = new JsonData ();
//小技能
JsonData Skill_Q = new JsonData ();
Skill_Q ["key"] = "Q";
Skill_Q ["SkillName"] = "大杀四方";
Skill_Q ["CD"] = "9/8/7/6";
Skill_Q ["MP"] = "30";
JsonData Skill_W = new JsonData ();
Skill_W ["key"] = "W";
Skill_W ["SkillName"] = "大杀四方";
Skill_W ["CD"] = "9/8/7/6";
Skill_W ["MP"] = "30";
JsonData Skill_E = new JsonData ();
Skill_E ["key"] = "Q";
Skill_E ["SkillName"] = "大杀四方";
Skill_E ["CD"] = "9/8/7/6";
Skill_E ["MP"] = "30";
JsonData Skill_R = new JsonData ();
Skill_R ["key"] = "R";
Skill_R ["SkillName"] = "大杀四方";
Skill_R ["CD"] = "9/8/7/6";
Skill_R ["MP"] = "30";
nuoks ["Skills"].Add (Skill_Q);
nuoks ["Skills"].Add (Skill_W);
nuoks ["Skills"].Add (Skill_E);
nuoks ["Skills"].Add (Skill_R);
string path = Application.dataPath + "/exercise/LitjsonStudy.txt";
//文件创建一个文本
FileStream file = new FileStream (path, FileMode.Create);
//得到字符串的UTF8数据流 引用using System.Text;
byte[] bts = System.Text.Encoding.UTF8.GetBytes (nuoks.ToJson ());
file.Write (bts, 0, bts.Length);
if (file != null) {
//清空缓冲
file.Flush ();
//关闭流
file.Close ();
//销毁资源
file.Dispose ();}
//刷新一下自动出现引用编辑器 using UnityEditor;
AssetDatabase.Refresh ();
解析就是如下
StreamReader reader = new StreamReader (Application.dataPath + "/exercise/LitjsonStudyTwo.txt");
string str = reader.ReadToEnd ();
reader.Close ();
reader.Dispose ();
JsonData date = JsonMapper.ToObject (str);
print (date ["skills"] [0] ["SkillName"]);