AssetBundle系列——共享资源打包/依赖资源打包
2015-07-21 15:34
513 查看
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数:
BuildPipeline.PushAssetDependencies():依赖资源压栈;
BuildPipeline.PopAssetDependencies():依赖资源出栈。
直接看代码,下面为打包示例代码,Prefab1和Prefab2共享贴图资源Tex1,在打包时将Tex1单独打包,而Prefab1和Prefab2对应的assetbundle包中不实际包含Tex1资源,而是记录Tex1资源的引用:
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class PushAndPop
{
[MenuItem("Test/BuildAssetBundle")]
static void Execute()
{
string SavePath = "C:\\";
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
| BuildAssetBundleOptions.DeterministicAssetBundle;
BuildPipeline.PushAssetDependencies();
// 共享资源Tex1.tga
Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/Tex1.tga");
BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
// Prefab1,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P1.prefab");
BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
// Prefab2,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab");
BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
EditorUtility.DisplayDialog("", "Completed", "OK");
AssetDatabase.Refresh();
}
}
可以看到,Push和Pos都是成对使用,一个Push/Pop对就相当于一个Layer(层),层可以嵌套,内层可以依赖外层的资源。也就是说内层某资源在打包时,如果其引用的某个资源已经在外层加载了,那么内层的这个资源包就会包含该资源的引用而不是资源本身。Push/Pop实际上维持了一个依赖的堆栈。
那么,在加载依赖资源包时,需要注意的是:先加载依赖的资源,然后加载其他资源,需要确保这个顺序。下面的代码演示如何使用依赖资源包:
先按第二个按钮,然后再按第三个按钮就能够正确显示两个Prefab,如果直接进行第三步操作,则实例化出来的Prefab会缺少贴图。
另外我们可以从assetbundle包的大小来看一下:
如果不打依赖包,两个prefab都打完整包,得到的包的大小为:
P1.assetbundle 56K
P2.assetbundle 38K
如果打依赖包,得到的包的大小为:
Tex1.assetbundle 10k
P1.assetbundle 47K
P2.assetbundle 29K
规律是不是很明显。
BuildPipeline.PushAssetDependencies():依赖资源压栈;
BuildPipeline.PopAssetDependencies():依赖资源出栈。
直接看代码,下面为打包示例代码,Prefab1和Prefab2共享贴图资源Tex1,在打包时将Tex1单独打包,而Prefab1和Prefab2对应的assetbundle包中不实际包含Tex1资源,而是记录Tex1资源的引用:
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class PushAndPop
{
[MenuItem("Test/BuildAssetBundle")]
static void Execute()
{
string SavePath = "C:\\";
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
| BuildAssetBundleOptions.DeterministicAssetBundle;
BuildPipeline.PushAssetDependencies();
// 共享资源Tex1.tga
Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/Tex1.tga");
BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
// Prefab1,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P1.prefab");
BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
// Prefab2,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab");
BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
EditorUtility.DisplayDialog("", "Completed", "OK");
AssetDatabase.Refresh();
}
}
可以看到,Push和Pos都是成对使用,一个Push/Pop对就相当于一个Layer(层),层可以嵌套,内层可以依赖外层的资源。也就是说内层某资源在打包时,如果其引用的某个资源已经在外层加载了,那么内层的这个资源包就会包含该资源的引用而不是资源本身。Push/Pop实际上维持了一个依赖的堆栈。
那么,在加载依赖资源包时,需要注意的是:先加载依赖的资源,然后加载其他资源,需要确保这个顺序。下面的代码演示如何使用依赖资源包:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { void OnGUI() { // 清空本地缓存 if (GUI.Button(new Rect(0f, 0f, 100f, 20f), Caching.spaceOccupied.ToString())) { Caching.CleanCache(); } if (GUI.Button(new Rect(0f, 30f, 100f, 20f), "Load Share Res")) { StartCoroutine(Load(@"file://C:\Tex1.assetbundle", 1)); } if (GUI.Button(new Rect(0f, 60f, 100f, 20f), "Load And Instantiate Prefab")) { StartCoroutine(LoadAndInstantiate(@"file://C:\P1.assetbundle", 1)); StartCoroutine(LoadAndInstantiate(@"file://C:\P2.assetbundle", 1)); } } // 加载 IEnumerator Load(string url, int version) { WWW www = WWW.LoadFromCacheOrDownload(url, version); yield return www; } // 加载并实例化 IEnumerator LoadAndInstantiate(string url, int version) { WWW www = WWW.LoadFromCacheOrDownload(url, version); yield return www; if (!System.String.IsNullOrEmpty(www.error)) { Debug.Log(www.error); } else { Object main = www.assetBundle.mainAsset; GameObject.Instantiate(main); } } }
先按第二个按钮,然后再按第三个按钮就能够正确显示两个Prefab,如果直接进行第三步操作,则实例化出来的Prefab会缺少贴图。
另外我们可以从assetbundle包的大小来看一下:
如果不打依赖包,两个prefab都打完整包,得到的包的大小为:
P1.assetbundle 56K
P2.assetbundle 38K
如果打依赖包,得到的包的大小为:
Tex1.assetbundle 10k
P1.assetbundle 47K
P2.assetbundle 29K
规律是不是很明显。
相关文章推荐
- 【玩转cocos2d-x之三十二】xml的解析
- java:多线程总结
- ECSHOP 广告添加搜索功能
- Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
- GRE写作必备句型
- Progress进度条
- struts2环境搭建
- 解决Chrome不支持本地Ajax请求的问题
- MFC编辑控件中改变字体大小
- mdadm创建software RAID
- 在pcDuino下编译安装openCV的问题
- tr1::function
- 【玩转cocos2d-x之三十一】弱联网与服务器的通讯
- Tesseract OCR(光学字符识别)教程
- ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)
- 顺序栈
- 指针的类型和指针所指向的类型
- C#实现给DataGrid单元行添加双击事件的方法
- 二叉树的基本应用:二叉树的遍历
- ArrayBlockingQueue跟LinkedBlockingQueue的区别