unity3d操作XML(C#)

在unity3D中使用xml的方法总结:

(使用c#)

1.需要引入一些包

using UnityEngine;
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections;

上面这些应该足够了

2.创建unity3D中的一组数据结构

比如我想要存入某一个物体的运动参数

public class MovementArgument
{
    public String Name;
    public String Tag;
    public float position_x;
    public float position_y;
    public float position_z;
    public float velocity_x;
    public float velocity_y;
    public float velocity_z;
}

3.再使用一个类来保存MovementArgument的对象,作为xml数据结构

public class UserData
{
    public MovementArgument _iUser
    public UserData()
	{
		_iUser = new MovementArgument();
	}
}

4.创建使用于本数据结构的存储读取过程(在底层的xml操作之上)

读取(其实读取操作与数据结构无关了,可以复用)

public void LoadData()
{
        StreamReader r  = File.OpenText(_FileLocation+"/"+ _FileName);//_FileLocation是unity3D当前project的路径名,_FileName是xml的文件名。定义为成员变量了
        //当然,你也可以在前面先判断下要读取的xml文件是否存在
        String _data=r.ReadLine();
        myData = DeserializeObject(_data) as UserData;//myData是上面自定义的xml存取过程中要使用的数据结构UserData
        r.Close();
}
存储(参数要根据需要变化)

public void SaveData(String sc1, String sc2,float sc3,float sc4,float sc5,float sc6,float sc7,float sc7)
{
        tempData._iUser.Name = sc1;//tempData是成员变量,UserData类型
        tempData._iUser.Tag = sc2;
        tempData._iUser.position_x = sc3;
        tempData._iUser.position_y = sc4;
        tempData._iUser.position_z = sc5;
        tempData._iUser.direction_x = sc6;
        tempData._iUser.direction_z = sc7;
        tempData._iUser.direction_y = sc8;

        StreamWriter writer  ;
        FileInfo t  = new FileInfo(_FileLocation+"/"+ _FileName);
        t.Delete();
        writer = t.CreateText();
        String _data = SerializeObject(tempData);//序列化这组数据
         writer.WriteLine(_data);//写入xml
         writer.Close();
}

5.底层的xml操作

底层xml读写使用.net中的System.xml命名空间中的类和方法,具体可见http://msdn.microsoft.com/zh-cn/library/gg145036.aspx

public String UTF8ByteArrayToString(byte []characters)
{
        UTF8Encoding encoding = new UTF8Encoding();
        String constructedString = encoding.GetString(characters);
        return (constructedString);
 }
 
 public byte[] StringToUTF8ByteArray(String pXmlString)
 {
         UTF8Encoding encoding = new UTF8Encoding();
        byte []byteArray = encoding.GetBytes(pXmlString);
        return byteArray;
 }

 // Here we serialize our UserData object of myData
 public String SerializeObject(object pObject)
 {
        String XmlizedString = "";
        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; // (MemoryStream)
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
 }

 // Here we deserialize it back into its original form
public 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);
 }



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值