当初记得这是个很重要的功能来着,写完就忘记是什么用了
一个保存一个读取;
using System;
using System.Collections.Generic;using System.IO;
using System.Xml;
using UnityEngine;
string saveAnimationPath = Application.dataPath + "/Resources/Scene/TeachFire1/Script/CameraState.xml";
public void SaveCameraPositionToXML(Transform tran)
{
//检测xml是否存在
if (File.Exists(saveAnimationPath))
{
//新建实例
XmlDocument xmlDoc = new XmlDocument();
//根据路径将xml读取出来
xmlDoc.Load(saveAnimationPath);
//得到根节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
//遍历所有子节点
foreach (XmlElement xe in nodeList)
{
if (xe.Name != tran.name)
continue;
foreach (XmlElement x2 in xe.ChildNodes)
{
//更新值
if (x2.Name == "ClipName") x2.InnerText = tran.name;
if (x2.Name == "PositionX") x2.InnerText = tran.position.x.ToString();
if (x2.Name == "PositionY") x2.InnerText = tran.position.y.ToString();
if (x2.Name == "PositionZ") x2.InnerText = tran.position.z.ToString();
if (x2.Name == "QuaternionX") x2.InnerText = tran.rotation.x.ToString();
if (x2.Name == "QuaternionY") x2.InnerText = tran.rotation.y.ToString();
if (x2.Name == "QuaternionZ") x2.InnerText = tran.rotation.z.ToString();
if (x2.Name == "QuaternionW") x2.InnerText = tran.rotation.w.ToString();
}
}
Debug.Log("SaveXML OK!");
xmlDoc.Save(saveAnimationPath);
}
else
{
throw new NullReferenceException("No XML");
}
}
public void ReadCameraPositionToXML(Transform tran)
{
float px, py, pz;
float rx, ry, rz, rw;
rx = ry = rz = rw = px = py = pz = 0;
//检测xml是否存在
if (File.Exists(saveAnimationPath))
{
//新建实例
XmlDocument xmlDoc = new XmlDocument();
//根据路径将xml读取出来
xmlDoc.Load(saveAnimationPath);
//得到根节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
//遍历所有子节点
foreach (XmlElement xe in nodeList)
{
if (xe.Name != tran.name)
continue;
foreach (XmlElement x2 in xe.ChildNodes)
{
//更新值
if (x2.Name == "ClipName") tran.name = x2.InnerText;
if (x2.Name == "PositionX") px = float.Parse(x2.InnerText);
if (x2.Name == "PositionY") py = float.Parse(x2.InnerText);
if (x2.Name == "PositionZ") pz = float.Parse(x2.InnerText);
tran.position = new Vector3(px, py, pz);
if (x2.Name == "QuaternionX") rx = float.Parse(x2.InnerText);
if (x2.Name == "QuaternionY") ry = float.Parse(x2.InnerText);
if (x2.Name == "QuaternionZ") rz = float.Parse(x2.InnerText);
if (x2.Name == "QuaternionW") rw = float.Parse(x2.InnerText);
tran.rotation = new Quaternion(rx, ry, rz, rw);
}
}
Debug.Log("RecordXML OK!");
}
else
{
throw new NullReferenceException("No XML");
}
}