找回密码
 立即注册
楼主: Unity联盟

[脚本] Unity3D读取保存XML文件

[复制链接]
发表于 2012-11-10 11:08 | 显示全部楼层 |阅读模式
/* Unity3D读取保存XML文件
  代码已测试,无误。就一个脚本。
  运行时会首先执行FirstSave()函数,该函数的作用是初始化XML里的内容,如果该XMl不存在,则会自动创建并初始化。
  
  按下save按钮后执行Save()函数会把数据保存到指定的XMl里
  
  按下load按钮后会执行Load()函数把你保存在XML里的数据读取出来
  
  导出exe后XML文件会保存在Data文件夹里,修改XML里面的数据,运行,你会发现读取的数据是你刚刚修改过的数据。
  
  想在unity项目中使用配置文件或者需要保存和读取数据但又不想使用数据库的可以看看
  
  可以保存和读取的数据类型支持英文、中文、浮点型、整型 */

[code=javascript]    import System;
  import System.Xml;
  import System.Xml.Serialization;
  import System.IO;
  import System.Text;

  class CeshiData{
   var Ceshi1 : String;
   var Ceshi2 : String;
   var Ceshi3 : float;
   var Ceshi4 : int;
  }

   class UserData
  {
   public var _iUser : CeshiData = new CeshiData();
   function UserData() { }
  }

  private var c1 : String;
  private var c2 : String;
  private var c3 : float;
  private var c4 : int;
  private var _FileLocation : String;
  private var _FileName : String = "CeshiData.xml";

  var myData : UserData[];
  private var tempData : UserData = new UserData();

  var i : int = 0;

  var GUISkin1 : GUISkin;
  var ShowData : int = 0;
  function Awake(){
   _Filelocation=Application.dataPath;
  }

  function Start(){
   FirstSave();
  }

  function FirstSave(){//初始化XML
   tempData._iUser.Ceshi1 = "?";
   tempData._iUser.Ceshi2 = "?";
   tempData._iUser.Ceshi3 = 0;
   tempData._iUser.Ceshi4 = 0;
   var writer : StreamWriter;
   var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
   if(!t.Exists)
   {
   writer = t.CreateText();
   _data = SerializeObject(tempData);
   for(i=0;i<10;i++){
   writer.WriteLine(_data);
   }
   writer.Close();
   }

  }

  function Save(sc1 : String,sc2 : String,sc3 : float,sc4 : int){//保存数据到指定的XMl里

   tempData._iUser.Ceshi1 = sc1;
   tempData._iUser.Ceshi2 = sc2;
   tempData._iUser.Ceshi3 = sc3;
   tempData._iUser.Ceshi4 = sc4;

   var writer : StreamWriter;
   var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
   t.Delete();
   writer = t.CreateText();
   _data = SerializeObject(tempData);
   for(i=0;i<10;i++){
   writer.WriteLine(_data);
   }
   writer.Close();

  }

  function Load(){//读取保存在XML里的数据
   var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
   var _info : String ;
   for(i=0;i<10;i++){
   _info = r.ReadLine();
   _data=_info;
   myData<i> = DeserializeObject(_data);
   }
   r.Close();
  }

  function OnGUI() {
   GUI.skin = GUISkin1;
   if(GUI.Button(Rect(0,0,100,40),"save")){
   Save("ceshi1","测试2",1.23,50);//要显示中文需设定中文字体
   }
   if(GUI.Button(Rect(200,0,100,40),"load")){
   Load();
   ShowData = 1;
   }
   if(ShowData == 1){
   GUI.Label(Rect(170,170+53*0,150,50),myData[0]._iUser.Ceshi1);
   GUI.Label(Rect(370,170+53*0,150,50),myData[0]._iUser.Ceshi2);
   GUI.Label(Rect(550,170+53*0,150,50),myData[0]._iUser.Ceshi3 + "");
   GUI.Label(Rect(760,170+53*0,150,50),myData[0]._iUser.Ceshi4 + "");

   GUI.Label(Rect(170,170+53*1,150,50),myData[1]._iUser.Ceshi1);

   GUI.Label(Rect(370,170+53*2,150,50),myData[2]._iUser.Ceshi2);

   GUI.Label(Rect(550,170+53*3,150,50),myData[3]._iUser.Ceshi3 + "");

   GUI.Label(Rect(760,170+53*4,150,50),myData[4]._iUser.Ceshi4 + "");
   }
  }

  //================================================================================
  function UTF8ByteArrayToString(characters : byte[] )
  {
   var encoding : UTF8Encoding = new UTF8Encoding();
   var constructedString : String = encoding.GetString(characters);
   return (constructedString);
  }

  //byte[] StringToUTF8ByteArray(string pXmlString)
  function StringToUTF8ByteArray(pXmlString : String)
  {
   var encoding : UTF8Encoding = new UTF8Encoding();
   var byteArray : byte[] = encoding.GetBytes(pXmlString);
   return byteArray;
  }

   // Here we serialize our UserData object of myData
   //string SerializeObject(object pObject)
  function SerializeObject(pObject : Object)
  {
   var XmlizedString : String = null;
   var memoryStream : MemoryStream = new MemoryStream();
   var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
   var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
   xs.Serialize(xmlTextWriter, pObject);
   memoryStream = xmlTextWriter.BaseStream; // (MemoryStream)
   XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
   return XmlizedString;
  }

   // Here we deserialize it back into its original form
   //object DeserializeObject(string pXmlizedString)
  function DeserializeObject(pXmlizedString : String)
  {
   var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
   var memoryStream : MemoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
   var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
   return xs.Deserialize(memoryStream);
  }
[/code]

评分

参与人数 1鲜花 +1 收起 理由
aa1287537617 + 1

查看全部评分

发表于 2013-1-18 14:53 | 显示全部楼层
楼主太有才了,膜拜中……
发表于 2013-5-17 16:19 | 显示全部楼层
楼主你好我想请教下,一运行就显示的这句是什么意思Assets/houjiarudaima/NewBehaviourScript.cs(1,8): error CS0116: A namespace can only contain types and namespace declarations
发表于 2013-7-11 11:32 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2017-6-19 21:40 | 显示全部楼层
很不错
发表于 2017-6-19 21:18 | 显示全部楼层
楼主是超人
发表于 2017-6-19 21:59 | 显示全部楼层
难得一见的好帖
发表于 2017-6-19 21:18 | 显示全部楼层
说的非常好
发表于 2017-6-19 21:59 | 显示全部楼层
很好哦
发表于 2017-6-27 09:10 | 显示全部楼层
很不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 10:22 , Processed in 0.105061 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表