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. [转]深入理解ajax系列——头部消息

    每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用.XHR对象提供了操作头部信息的方法.本文将详细介绍HTTP的头部信息 默认信息 默认情况下,在发送XHR请求的同时,还会发送下列头 ...

  2. css3动画性能优化

    css3的动画简单好用,但是性能方面存在一些问题,很多时候一不留神cpu就已经满了. 现在记下一些常用的技巧,去优化我们的css3的动画. 1. translate3d进行gpu加速 写动画的时候写个 ...

  3. MAC+iTerm定制目录显示颜色和提示符

    知道该如何定制ls时各种类型文件(unix下所有的都是file..)的颜色了. 很简单,就是在.bash_profile下加了三行. export CLICOLOR=1 export LSCOLORS ...

  4. Java Servlet实现下载文件

    一.配置servlet 在WebContent(以前的eclipse版本是WebRoot)文件夹下,有一个web.xml 修改web.xml ,加入以下代码 <servlet> <s ...

  5. jframe 设置左上角和任务栏的图标

    默认就是 改成有意义的,一眼就能看出来功能的,比如一个小蜘蛛 第一个最简单的做法,把图片扔到工程的根目录,但是这样会相当乱,不便于文件管理 ImageIcon icon = new ImageIcon ...

  6. JS Ajax跨域访问

    js ajax跨域访问报"No 'Access-Control-Allow-Origin' header is present on the requested resource 如果请求的 ...

  7. PHP通过sql生成CSV文件并下载,PHP实现文件下载

    /** * PHP通过sql生成CSV文件并下载 * @param string $sql 查询sql,结果为二维数组 * @param array $title 数据,CSV文件标题 * @para ...

  8. bzoj 1024 [SCOI2009]生日快乐——模拟

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1024 可以枚举这边放多少块.那边放多少块. 注意精度.不要每次用x*y/base算有多少块, ...

  9. PHP获取用户客户端真实IP的解决方案是怎样呢?

    function getIp(){if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIE ...

  10. RGBA(0,0,0,0)调色

    前三个值(红绿蓝)的范围为0到255之间的整数或者0%到100%之间的百分数.这些值描述了红绿蓝三原色在预期色彩中的量. 第四个值,alpha值,制定了色彩的透明度/不透明度,它的范围为0.0到1.0 ...