標籤:ring sel refresh node 包含 value 3.5 heron 列印
用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"]);
XML資料 JSON資料 LitJSON 資料 的編寫和解析 小結