Unity的unity 不序列化化配表问题用什么方法比较好

公众号:woxueyuan_com
我学院在线教育平台,中国IT互联网游戏开发、游戏设计免费在线教育平台。学游戏就上我学院。
感谢您阅读Unity序列化高级篇及应用篇 ,本文可能来自网络,如果侵犯了您的相关权益,请联系管理员。QQ:关注游戏葡萄
微信扫描二维码关注
游戏葡萄公众号
的其他文章
的其他文章
的其他文章
的其他文章
的其他文章
的其他文章
TalkingData
的其他文章
的其他文章
的其他文章
All Rights Reserved
赞助Sponsor
赞助Sponsor
阅读Articles
数据库Data
数据库Data
招聘Recruitment
联系我们Contact
友情链接Links
游戏葡萄订阅号在Unity3D中使用ScriptableObject脚本对象化进行序列化
发布时间: 10:32:59
ScriptableObject类型经常用于存储一些unity3d本身不可以打包的一些object,比如字符串,一些类对象等。用这个类型的 子类型,则可以用BuildPipeline打包成assetbundle包供后续使用,非常方便。这是除了PlayerPrefs和c# XML文件读取外,另外 的一种存取一些数据对象的方法
using&UnityE&&&
public&class&XMLContains&:&ScriptableObject&&&
&&&&public&string&theXML;&&&
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:通过使你的类从ScriptableObject脚本对象化这个类派生,使你的类称为一种资源类型,这样的话,你可以在硬盘上存储他们,并使用unity3d的IDE接口去资源文件拖拽他们的行为或者任何其他。
using&UnityE&&&
public&class&TieFighter&:&MonoBehaviour&&&
&&&&&public&XMLContainer&myXMLS&&&
&&&&&void&Awake()&&&
&&&&&&&&//parse&myXMLSettings.theXML&into&reasonable&data&&&
&&&&&void&Update()&&&
&&&&&&&&//use&the&reasonable&data&&&
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、先创建一个类:
using&UnityE&&&
using&System.Collections.G&&&&
public&class&SysData&:&ScriptableObject&&&
{&&&&&&&&&&&&&&&&&
public&List&Vector3&&&&&
该类只有一个属性content。当然也可以声明更多属性、事件和方法。
在后面,这个类的实例将作为资源被打包。
2、创建一个编辑器脚本:
该脚本用于实例化SysData,设置一些数据,然后打包。
using&UnityE&&&
using&UnityE&&&
using&System.Collections.G&&&&
public&class&Export&&
{&&&&&&&&&&
&&[MenuItem("Assets/Export")]&&&&&&&&&&&
&&public&static&void&Execute()&&&
&&{&&&&&&&&&&&&&&&&
&&&//实例化SysData&&&&&&&&&&&&&&&
&&&SysData&sd&=&ScriptableObject.CreateInstance&SysData&();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&//随便设置一些数据给content&&&&&&&&&&&&&&&&&
&&&&sd.content&=&new&List&Vector3&();&&&&&&&&&&&&&&&&&
&&&&sd.content.Add(new&Vector3(1,2,3));&&&&&&&&&&&&&&&&&
&&&&sd.content.Add(new&Vector3(4,5,6));&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&//&SysData将创建为一个对象,这时在project面板上会看到这个对象。&&&&&&&&&&&&&&&&&
&&&&string&p&=&"Assets/SysData.asset";&&&&&&&&&&&
&&&&AssetDatabase.CreateAsset(sd,&p);&&&&&&&&&&&&&&&&&
&&&&Object&o&=&AssetDatabase.LoadAssetAtPath(p,&typeof(SysData));&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&//打包为SysData.assetbundle文件。&&&&&&&&&&&&&&&&&
&&&&BuildPipeline.BuildAssetBundle(o,&null,&"SysData.assetbundle");&&&&&&&&&&&&&&&&&
&&&&//删除面板上的那个临时对象&&&&&&&&&&&&&&&
&&&&&AssetDatabase.DeleteAsset(p);&&&&&&&&&&&&&&&&&&&&&&&&
3、运行时加载这个数据资源。
IEnumerator&Start&()&&&
{&&&&&&&&&&&&&&&&
&WWW&www&=&new&WWW("file://"&+&Application.dataPath&+&"/../SysData.assetbundle");&&&&&&&&&&&&&&&&
&yield&return&&&&&
&&&&&&&&&&&&&&&
&//转换资源为SysData,这个sd对象将拥有原来在编辑器中设置的数据。&&&&&&&&&&&&&&&&
&SysData&sd&=&www.assetBundle.mainAsset&as&SysD&&&
&//如打印sd.content[0],将得到Vector3(1,2,3);&&&&&&&&&&&&&&&
&print(sd.content[0]);&&
来源:/oldman/articles/2396051.html

我要回帖

更多关于 unity 序列化有什么用 的文章

 

随机推荐