纳金网

标题: 在Unity3D中使用ScriptableObject进行序列化 [打印本页]

作者: star-小虎    时间: 2013-10-12 15:23
标题: 在Unity3D中使用ScriptableObject进行序列化
ScriptableObject类型经常用于存储一些unity3d本身不可以打包的一些object,比如字符串,一些类对象等。用这个类型的子类型,则可以用BuildPipeline打包成assetbundle包供后续使用,非常方便。这样除了playerpref和c#文件读取外,另外的一种存取一些数据对象的方法
[csharp] view plaincopyprint?



By making your class enherit from ScriptableObject, it becomes an "Asset Type" to unity, in the sense that you can store them on disk, and use the Unity IDE interface to drag it from the assetsfolder to a behaviour or whatever. It also automatically plays nice with Unity's dependency tracking system that decides what does and what does not go into a webplayer. Going a bit further with this example, you could have let's say a TieFighter class, actually get some of its information (max health, max speed, acceleration), from your xml file, by making the TieFighter class look like this:

[csharp] view plaincopyprint?



You could then attach the TieFighter script to a gameobject, and then drag your xml-asset from your assetsfolder onto the myXMLSettings public field.Please note that this example is much better at showing how you could use a ScriptableObject than how to properly implement a TieFighter, as I would never really have that parse an actual xml file on runtime. Tiefighters are too cool for xml anyway.
以上是不做成bundle包的形式,下面提供了一种打包成bundle资源,通过www来进行载入的方案:
通常,可以用BuildPipeline.BuildAssetBundle打包资源,这些资源包括模型、纹理、贴图等。其实也可以打包ScriptableObject,而ScriptableObject中可以存储任何数据类型。
以下为一个完整示例。
1、先创建一个类:
[csharp] view plaincopyprint?



该类只有一个属性content。当然也可以声明更多属性、事件和方法。
在后面,这个类的实例将作为资源被打包。
2、创建一个编辑器脚本:
该脚本用于实例化SysData,设置一些数据,然后打包。
[csharp] view plaincopyprint?



3、运行时加载这个数据资源。
[csharp] view plaincopyprint?



4、这其实是把整个对象实例打包,如果ScriptableObject属性中包括其他的类实例,则这些类需加上[Serializable]序列化。通过这种方法,可以将系统需要的数据(如角色分类、matrix4x4数据等等)打包,加载后直接转换为预定的类型,具有更高的效率。


作者: 咻咻神    时间: 2013-10-12 16:46
顶一个!!
作者: nts    时间: 2013-10-13 11:12
谢谢学习了
作者: nts    时间: 2013-10-13 11:15
序列化应该是网络中才用的技术吧




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5