using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text; public class _GameSaveLoad: MonoBehaviour { // An example where the encoding can be found is at
// http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp
// We will just use the KISS method and cheat a little and use
// the examples from the web page since they are fully described // This is our local private members
Rect _Save, _Load, _SaveMSG, _LoadMSG;
bool _ShouldSave, _ShouldLoad,_SwitchSave,_SwitchLoad;
string _FileLocation,_FileName;
public GameObject _Player;
UserData myData;
string _PlayerName;
string _data; Vector3 VPosition; // When the EGO is instansiated the Start will trigger
// so we setup our initial values for our local members
void Start () {
// We setup our rectangles for our messages
_Save=new Rect(10,80,100,20);
_Load=new Rect(10,100,100,20);
_SaveMSG=new Rect(10,120,400,40);
_LoadMSG=new Rect(10,140,400,40); // Where we want to save and load to and from
_FileLocation=Application.dataPath;
_FileName="SaveData.xml"; // for now, lets just set the name to Joe Schmoe
_PlayerName = "Joe Schmoe"; // we need soemthing to store the information into
myData=new UserData();
} void Update () {} void OnGUI()
{ //***************************************************
// Loading The Player...
// **************************************************
if (GUI.Button(_Load,"Load")) { GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
// Load our UserData into myData
LoadXML();
if(_data.ToString() != "")
{
// notice how I use a reference to type (UserData) here, you need this
// so that the returned object is converted into the correct type
myData = (UserData)DeserializeObject(_data);
// set the players position to the data we loaded
VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);
_Player.transform.position=VPosition;
// just a way to show that we loaded in ok
Debug.Log(myData._iUser.name);
} } //***************************************************
// Saving The Player...
// **************************************************
if (GUI.Button(_Save,"Save")) { GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
myData._iUser.x=_Player.transform.position.x;
myData._iUser.y=_Player.transform.position.y;
myData._iUser.z=_Player.transform.position.z;
myData._iUser.name=_PlayerName; // Time to creat our XML!
_data = SerializeObject(myData);
// This is the final resulting XML from the serialization process
CreateXML();
Debug.Log(_data);
} } /* The following metods came from the referenced URL */
string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
} byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
} // Here we serialize our UserData object of myData
string SerializeObject(object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(UserData));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
} // Here we deserialize it back into its original form
object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
} // Finally our save and load methods for the file itself
void CreateXML()
{
StreamWriter writer;
FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("File written.");
} void LoadXML()
{
StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
string _info = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("File Read");
}
} // UserData is our custom class that holds our defined objects we want to store in XML format
public class UserData
{
// We have to define a default instance of the structure
public DemoData _iUser;
// Default constructor doesn't really do anything at the moment
public UserData() { } // Anything we want to store in the XML file, we define it here
public struct DemoData
{
public float x;
public float y;
public float z;
public string name;
}
}

Save and Load from XML的更多相关文章

  1. error opening trace file: No such file or directory (2) ,can't load transform_config.xml

    出现这个错误:error opening trace file: No such file or directory (2) ,can't load transform_config.xml 是因为没 ...

  2. [置顶] .NET下枚举类型的Save和Load分析

    今天在写代码的时候,心血来潮对原来的字符串保存状态位的方式很不满意,对于代码里出现了 state == "1" 这样的状态判断很是不爽.那么理想中的判断是怎样的呢?很简单如你所想枚 ...

  3. docker~save与load的使用

    回到目录 对于没有私有仓库来说,将本地镜像放到其它服务器上执行时,我们可以使用save和load方法,前者用来把镜像保存一个tar文件,后台从一个tar文件恢复成一个镜像,这个功能对于我们开发者来说还 ...

  4. [转帖]Docker save and load镜像保存

    Docker save and load镜像保存 https://www.cnblogs.com/zhuochong/p/10064350.html docker save 和 load 以及 imp ...

  5. dokcer 的export 、improt和save 、load

    export .improt 是对容器操作也就是类似于虚拟机的快照 save .load 是针对于镜像操作的..

  6. docker save docker load

    docker save && docker load docker save 镜像1 镜像2 | gzip > images.tar.gz 打包镜像为压缩文件 docker sa ...

  7. Docker save and load,镜像保存

    一.起因 docker pull 时发生错误 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/r ...

  8. 详解Pytorch中的网络构造,模型save和load,.pth权重文件解析

    转载:https://zhuanlan.zhihu.com/p/53927068 https://blog.csdn.net/wangdongwei0/article/details/88956527 ...

  9. Docker(十三)-Docker save and load镜像保存

    持久化docker的镜像或容器的方法 Docker的镜像和容器可以有两种方式来导出 docker save #ID or #Name docker export #ID or #Name docker ...

随机推荐

  1. 使用线程 Monitor.Wait() 和 Monitor.Pulse()

      Wait() 和 Pulse() 机制用于线程间交互.当在一个对象上使用Wait() 方法时,访问这个对象的线程就会一直等待直到被唤醒.Pulse() 和 PulseAll() 方法用来通知等待的 ...

  2. 区块链、云计算、大数据、人工智能、FinTech带来的挑战与机遇,中国技术开放日上海站精彩回顾

    区块链.云计算.大数据.人工智能.FinTech带来的挑战与机遇,中国技术开放日上海站精彩回顾 | 作者 韩婷 发布于 2016年12月26日. 估计阅读时间: 不到一分钟 | 欲知区块链.VR.Te ...

  3. 避免SQL注入三慷慨法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangyy130/article/details/26154837       要说SQL注入还要从 ...

  4. 数据库安全 (ch.4)

    4.2.4 授权与回收 使用 Grant 授予权限 使用Revoke 回收权限 Grant [权限] ON *.. to * [with grant option] with grant option ...

  5. oracle基本认识

    概要图 1. 环境搭建 1.1 Oracle的安装 数据库的三个常用的用户及默认密码sys:change_on_installsystem:managerscott:tiger Oracle客户端: ...

  6. Mac 下终端命令行之基本命令总结(持续更新)

    最近用Mac做一些开发,用到了一些命令行的内容,先将常用的命令行进行总结.由于会不断的用到新的,所以将会持续的总结进来.每一个命令行的使用可能都会比较复杂,我只会总结最常用的使用方法. echo命令 ...

  7. 入职9月,旷视孙剑106分钟讲述CV创业科研的5大区别

    雷锋网按:本文为旷视科技首席科学家孙剑日前在 CCF-ADL上做的题为<如何在大公司和创业公司做好计算机视觉研究>的分享,主要介绍了近期计算机视觉的发展现状,ResNet基本原理和设计,旷 ...

  8. 软工作业———Alpha版本第二周小结

    姓名 学号 周前计划安排 每周实际工作记录 自我打分 zxl 061425 1.进行任务分配2.实现扫码和生成二维码功能 1.对主要任务进行了划分,但还为进行给模块间的联系2.完成了扫码签到功能 90 ...

  9. B站直播 DEMO ijkplayerDemo

    http://blog.csdn.net/kengsir/article/details/51750879 一. 下载ijkplayer ijkplayer下载地址 下载完成后解压, 解压后文件夹内部 ...

  10. 【JZOJ4925】【GDOI2017模拟12.18】稻草人

    题目描述 YLOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,YLOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下条 ...