Unity 数据管理 [转自羽化]
这篇文章很好,值得收藏。
原文地址:http://blog.csdn.net/libeifs/article/details/7310962
正文:
Unity 数据管理
开发环境
Window7
Unity3D 3.5.0
MB525defy Android 2.2.1
本次学习:
1. 简介物件组成
2. AssetBundle
3. 打包和读取
1.简介物件组成
这是羽化做的一个粗略图,其中各个组成部分除了工程以外都能通过AssetBundle打成一个后缀名为.unity3d的外部资源包,通过Unity自身的高压技术,最多能节约60%~70%的资源容量。
2.AssetBundle
官方有自带的AssetBundle的
例子,羽化通过这个例子改成了自己的一个小工程,通过AssetBundle的打包处理的好处在于把自身的依赖关系包括数据结构合理的分配完整,这样不需
要开发者自己去给下载或者加载的资源控制路径,大大降低了开发大型在线游戏的门槛,同时越来越细的封包可以减少游戏的总体大小,资源最大化利用,对于小游
戏也带来了很大帮助,可惜按照官方的说法是目前不能完全支持移动端。
AssetBundle 具体使用方法在手册上写得很详细,圣典里也有详细的中文说明,羽化就不多说废话,需要注意的是AssetBundle的读取,虽然资源基本上都是用简单读 取,但在特殊情况下会去读取特定unity3d包中的特定贴图,或者说是模型,这里就必须注意打包的结构,因为读取出来的是一个Object数组,可以使 用Load读取特定资源。
3. 打包和读取
打包方式是在Project视图下选择AutomatedExportResources,过程是自动完成,Unity的Editor类中提供了很多有趣的方法可以实现不同的打包操作,官方只给了手选打包和自动打包两种,但都不够智能。
AutomatedExportResources.js
- <span style="font-size:16px;">@MenuItem("Assets/Auto Build Resource Files")
- static function ExportResource ()
- {
- System.IO.Directory.CreateDirectory("AssetBundles");
- var options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;
- BuildPipeline.PushAssetDependencies();
- /*Effects*/
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit1.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit2.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit1.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit2.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit1.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit2.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit1.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit2.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PopAssetDependencies();
- }</span>
这是羽化的打包代码,可以看出羽化做了一个两层依赖,当然可以做更多,但写起也就更加复杂,这种打包实现的是预设与其他物件分离,其他物件包括材质球、模
型、贴图,材质球中包含着色器和属性信息,贴图往往是一张tga,模型是一个FBX,这样打包羽化认为基本上是最省配置,但要注意加载的时候,在有预设的
情况下,最后先下载材质球再下载模型。
Prefab.js
- <span style="font-size:16px;">var download : WWW;
- var url = "packed_resource.unity3d";
- var resourcePath = "Lerpz";
- var guiOffset = 20;
- var assetBundle : AssetBundle;
- var instanced : Object;
- function StartDownload () {
- if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
- download = new WWW (url);
- else if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
- download = new WWW ("../AssetBundles/" + url);
- else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
- download = new WWW ("file://" + Application.dataPath + "/../AssetBundles/" + url);
- yield download;
- assetBundle = download.assetBundle;
- if (assetBundle != null)
- {
- // Alternatively you can also load an asset by name (assetBundle.Load("my asset name"))
- var go : Object = assetBundle.mainAsset;
- if (go != null)
- {
- instanced = Instantiate(go);
- }
- else
- Debug.Log("Couldnt load resource");
- }
- else
- {
- Debug.Log("Couldnt load resource");
- }
- }
- function OnGUI()
- {
- GUILayout.Space(guiOffset);
- GUILayout.BeginHorizontal();
- if (download == null)
- {
- if (GUILayout.Button("Download " + url))
- StartDownload();
- }
- else
- {
- if (download.error == null)
- {
- var progress = parseInt(download.progress * 100);
- GUILayout.Label(progress + "%");
- if (download.isDone && GUILayout.Button("Unload Resource") )
- {
- // Destroy the instantiated object
- Destroy(instanced);
- // Dispose the WWW class
- // (This happens automatically from the GC, but you can do it explicitly to make sure it happens early on)
- download.Dispose();
- download = null;
- // Unload the whole asset bundles and any loaded assets
- assetBundle.Unload (true);
- assetBundle = null;
- }
- }
- else
- {
- GUILayout.Label(download.error);
- }
- }
- GUILayout.EndHorizontal();
- }</span>
预设下载和其他下载差不多,只是预设中多了一步实例化。
项目下载地址:(附送“FF13 - 誓言”)