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. Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)

    在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...

  2. E浏览器常见的9个css Bug以及解决办法

    我们在浏览网页的时候经常看见这样的现象:某个网页在IE6浏览器中打开很正常,但是在IE8里面打开可能完全变形了.或者也有可能出现完全相反的现象.这让Web程序员及设计师往往为了其CSS在各个IE版本下 ...

  3. day18 10.使用ThreadLocal来解决问题

    ThreadLocal是一个容器/集合,是一个Map集合.不管你跨多少层,只要你是同一个线程就可以取出来.Service和Dao是同一个线程.Service第一次调用JdbcUtils.getConn ...

  4. 2019-9-2-windows-10「设置」应用完整ms-settings快捷方式汇总

    title author date CreateTime categories windows-10「设置」应用完整ms-settings快捷方式汇总 lindexi 2019-09-02 12:57 ...

  5. C++ operator new和new operator的区别

    new operator 当你写这种代码: string *ps = new string("Memory Management"); 你使用的new是new  operator. ...

  6. 介绍vue项目中的axios请求(get和post)

    一.先安装axios依赖,还有qs依赖 npm install axios --save npm install qs --save qs依赖包用post请求需要用到的 插入一个知识点: npm in ...

  7. SQL优化系列(三)- 用最少的索引获得最大的性能提升

    从全局出发优化索引 对于高负载的数据库,如何创建最少的索引,让数据库的整体性能提高呢?例如,对于100 条SQL语句,如何创建最佳的5条索引? SQL自动优化工具SQL Tuning Expert P ...

  8. 【JZOJ3854】【NOIP2014八校联考第2场第2试9.28】分组(group)

    MEi Bsny所在的精灵社区有n个居民,每个居民有一定的地位和年龄,ri表示第i个人的地位,ai表示第i个人的年龄. 最近社区里要举行活动,要求几个人分成一个小组,小组中必须要有一个队长,要成为队长 ...

  9. Redis、MPP、kafka 、MongDB简介

    Redis :间值数据库,适合缓存用户Session会话与经常需要查的数据1.Redis集群,为什么在项目中使用集群  1.持久化,持久化是最简单的高可用方法(有时甚至不被归为高可用的手段),主要左右 ...

  10. 比较全面的一个PHP缓存类解析

    转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...