Unity3d Serialize问题
备忘:
1. ScriptableOjbect中,由于Serialization的原因,不能使用基类引用来存储子类对象,这样都会导致数据丢失
2. 无法直接对Unity的数据如,vector3, quaternion等使用BinaryFormatter进行序列化,会报“SerializationException: Type UnityEngine.Vector3 is not marked as Serializable”。要绕过这个问题的姿势是,自己对Vector3进行包装。下面是其中一种方法
using System;
using System.Runtime.Serialization;
using UnityEngine; namespace Assets.Editor
{
sealed class Vector3SerializationSurrogate : ISerializationSurrogate
{ public void GetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
Vector3 v3 = (Vector3)obj;
info.AddValue("x", v3.x);
info.AddValue("y", v3.y);
info.AddValue("z", v3.z);
} public object SetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISurrogateSelector selector)
{
Vector3 v3 = (Vector3)obj;
v3.x = (float)info.GetValue("x", typeof(float));
v3.y = (float)info.GetValue("y", typeof(float));
v3.z = (float)info.GetValue("z", typeof(float)); return (object)v3;
}
}
}
public static void TestFunc()
{
BinaryFormatter bf = new BinaryFormatter();
SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3Surrogate = new Vector3SerializationSurrogate();
ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3Surrogate); bf.SurrogateSelector = ss; MemoryStream stream = new MemoryStream(); Vector3 testObj = new Vector3(, , );
bf.Serialize(stream, testObj);
stream.Position = ;
Vector3 resultObj = (Vector3)bf.Deserialize(stream);
Debug.Log(resultObj);
}
Unity3d Serialize问题的更多相关文章
- 使用 Sublime Text 2 开发 Unity3D 项目
用 Sublime 已经有很长一段时间,很舒适,很贴心,根本停不下来.之前因为是开发页游,所以是用 AS3 开发,近段时间,新开了个手游项目,引擎方面选定了 Unity3D,老实说,之前没有太多的 3 ...
- unity3d优化总结篇
转自http://www.unitymanual.com/thread-21597-1-1.html 此总结由自己经验及网上收集整理优化内容 包括:1.代码方面:2.函数使用方面:3.ngui注意方面 ...
- [转]unity3d 脚本参考-技术文档
unity3d 脚本参考-技术文档 核心提示:一.脚本概览这是一个关于Unity内部脚本如何工作的简单概览.Unity内部的脚本,是通过附加自定义脚本对象到游戏物体组成的.在脚本对象内部不同志的函数被 ...
- Unity3D常用代码总结
1 GUI汇总 function OnGUI() { GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1 GUI.Box(Rect(1,2 ...
- Unity3D 游戏开发构架篇 ——角色类的设计与持久化
在游戏开发中,游戏角色占了很大的篇幅,可以说游戏中所有的内容都是由主角所带动.这里就介绍一下角色类的设计和持久化. 一.角色类应用场景和设计思想 游戏中的角色类型不一而足,有不同的技能,有不同的属性等 ...
- Unity3D脚本中文系列教程(十)
http://dong2008hong.blog.163.com/blog/static/4696882720140312627682/?suggestedreading&wumii Unit ...
- Unity3D脚本中文系列教程(八)
◆ static var matrix : Matrix4x4 描述:设置用于渲染所有gizmos的矩阵. 类方法 ◆ Static function DrawCube(center:Vector3, ...
- Unity3D脚本中文系列教程(五)
http://dong2008hong.blog.163.com/blog/static/4696882720140302848544/?suggestedreading&wumii Unit ...
- 【转】使用unity3d需要注意到细节
原 文:http://cache.baiducontent.com /c?m=9d78d513d9841df41ea6837e7c01a6660e20f6743da7c76508c3e34f84152 ...
随机推荐
- Eclipse编辑jsp卡死解决方案
使用Eclipse编辑jsp.js文件时,经常出现卡死现象,在网上百度了N次,经过N次优化调整后,卡死现象逐步好转,具体那个方法起到作用,不太好讲.将所有用过的方法罗列如下: 1.取消验证 windo ...
- Grunt上手指南(转)
Grunt , javascript 我想先花点时间回忆一下作为一个前端需要做的工作(Loading...) JS合并 JS压缩 CSS压缩 CSS Sprite 图片优化 测试 静态资源缓存(版本 ...
- U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...
- dwz中弹出的窗口页面如何获取前页面(点击按钮的页面)的元素???
在页面A.jsp中点击一个按钮,使用$.pdialog.open()方法弹出b.jsp页面(对话框窗口),我要在b.jsp中选中值然后关闭窗口(b.jsp)返回值给A.jsp~ =========== ...
- JAVA EE 学习笔记
http://www.cnblogs.com/kuangdaoyizhimei/category/701794.html http://www.cnblogs.com/liunanjava/p/445 ...
- telnet协议的作用详解,以及telnet端口号介绍
转:http://www.ctowhy.com/382.html Telnet协议,工作在TCP/IP协议栈的“应用层”,telnet是一种使用命令行的远程终端管理的协议,可以远程连接到网络设备上,并 ...
- NET PROVIDER 连接 Oracle数据库
NET 数据库连接 ORacle http://www.devart.com/ DataDirect http://www.datadirect.com/ Oracle免客户端For . ...
- python-sdk-demo的打包
1.安装setuptools pip install python-setuptools 2.创建一个简单的包 下载demo https://github.com/cp-m/py-sdk-demo.g ...
- centos搭建git服务器(转)
一:git服务安装 1.安装git相关组件 [root@gitserver ~] yum -y install git 2.创建git用户 [root@gitserver ~] groupadd gi ...
- 《Hadoop基础教程》之初识Hadoop 【转】
Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...