|
资源信息 Tutorial Information
教程名称: | Unity3D读取保存XML文件(发帖教程) |
适用引擎: | (适用引擎,为空默认为Unity) |
教程语种: | 中文 |
教程等级: | 1 |
教程格式: | 图文(请用IE9以上浏览器访问本版块) |
教程作者: | 转载自互联网 (如有问题请短消息联系作者或发表回复) |
下载地址: | 无 (兑换积分) |
/* 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 = 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] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|