Unity YAML 序列化与反序列化

脚本必须放在Editor文件夹下才可以使用下面的引用,

using YamlDotNet.Serialization;

这个引用也是序列化所必须使用的;

 

代码如下:

using UnityEngine;
using System;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using YamlDotNet.Serialization;

public class ItemInfo
{
	private int id;
	private string name;
	public int ID
	{
		set{id = value;}
		get{return id;}
	}
	public string Name
	{
		set{name = value;}
		get{return name;}
	}
}

public class TestInfo
{
	private Rect m_position = new Rect();
	private List<ItemInfo> listInfo = new List<ItemInfo>();

	public float Xpos
	{
		get{return m_position.x;}
		set{m_position.x = value;}
	}

	public float Ypos
	{
		get{return m_position.y;}
		set{m_position.y = value;}
	}

	public float Width
	{
		get{return m_position.width;}
		set{m_position.width = value;}
	}

	public float Height
	{
		get{return m_position.height;}
		set{m_position.height = value;}
	}

	public List<ItemInfo> ListInfo
	{
		get{return listInfo;}
		set{listInfo = value;}
	}
}

public class SerializeObject {

	string filePath = "D:/序列化数据/1.txt";
	public static SerializeObject _instance = new SerializeObject();


	[MenuItem("H3D/序列化")]
	static void Init () 
	{
		_instance.Serializer ();
	}

	[MenuItem("H3D/反序列化")]
	static void Init2 () 
	{
		_instance.Deserializer ();
	}

	void Serializer()            // 序列化操作
	{
		object obj = GetSerializeObject();
		StreamWriter yamlWriter = File.CreateText (filePath);
		Serializer yamlSerializer = new Serializer();
		yamlSerializer.Serialize(yamlWriter, obj);
		yamlWriter.Close();
	}

	void Deserializer()           // 反序列化操作
	{
		if (!File.Exists(filePath))
		{
			return;
		}
		StreamReader yamlReader = File.OpenText(filePath);
		Deserializer yamlDeserializer = new Deserializer();
	
		//读取持久化对象
		TestInfo info = yamlDeserializer.Deserialize<TestInfo>(yamlReader);
		yamlReader.Close();
		PrintInfo (info);
	}


	public object GetSerializeObject()      // 获得序列化的数据
	{
		TestInfo newInfo = new TestInfo();
		newInfo.Xpos = 10;
		newInfo.Ypos = 10;
		newInfo.Width = 800;
		newInfo.Height = 600;

		List<ItemInfo> itemLists = new List<ItemInfo> ();
		ItemInfo info1 = new ItemInfo ();
		info1.ID = 1;
		info1.Name = "aa";
		itemLists.Add (info1);

		ItemInfo info2 = new ItemInfo ();
		info2.ID = 2;
		info2.Name = "bb";
		itemLists.Add (info2);

		newInfo.ListInfo = itemLists;

		return newInfo;
	}

	void PrintInfo(TestInfo info)         // 输出序列化的数据
	{
		Debug.Log (info.Xpos);
		Debug.Log (info.Ypos);
		Debug.Log (info.Width);
		Debug.Log (info.Height);
		foreach (var item in info.ListInfo)
		{
			Debug.Log (item.ID);
			Debug.Log (item.Name);
		}
	}
}


 

序列化后的效果图如下所示:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值