(转)AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数:
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
规律是不是很明显。
(转)AssetBundle系列——共享资源打包/依赖资源打包的更多相关文章
- AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- (转)[Unity3D]BuildPipeline.PushAssetDependencies 打包依赖包,优化UI Prefab的资源引用加载(坑爹之处)
转自:http://blog.csdn.net/chiuan/article/details/39040421#reply 1:长话短说,UI Prefab中一般会交叉引用Atlas,那么打包时候我们 ...
- AssetBundle系列——场景资源之打包(一)
本篇讲解的是3D游戏的场景资源打包方式,首先简单的分析一下场景中所包含的资源的类型. 场景资源一般包含:地表模型(或者是Unity Terrain),非实例化物体(摄像机.空气墙.光源.各种逻辑物体之 ...
- AssetBundle系列——场景资源之解包(二)
本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面: (1)加载场景 场景异步加载的代码比较简单,如下所示: private IEnumerator LoadLevelCoroutine( ...
- AssetBundle系列——资源的加载、简易的资源管理器
每个需要进行资源管理的类都继承自IAssetManager,该类维护它所使用到的所有资源的一个资源列表.并且每个资源管理类可以重写其资源引用接口和解引用接口. 每个管理器有自己的管理策略,比如Scen ...
- [Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题
[Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题 2017年07月05日 15:57:44 阅读数:1494 http://www.cpp ...
- AssetBundle打包依赖(宽宽又欠我一顿烧烤)
using UnityEngine; using System.Collections; using UnityEditor; public class dabao : EditorWindow { ...
- Webpack:前端资源模块化管理和打包工具
一.介绍: Webpack 是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生 产环境部署的前端资源.还可以将按需加载的模块进行代码分隔,等到实际需要的时候再 ...
- Android应用性能优化系列视图篇——隐藏在资源图片中的内存杀手
图片加载性能优化永远是Android领域中一个无法绕过的话题,经过数年的发展,涌现了很多成熟的图片加载开源库,比如Fresco.Picasso.UIL等等,使得图片加载不再是一个头疼的问题,并且大幅降 ...
随机推荐
- native与ascii互转
package org.edujks.schoolapp.schoolbasedresearch.schoolsubject.action; public class DecodeUtil { /** ...
- HDU 4813 Hard Code(水题,2013年长春现场赛A题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4813 签到题. 把一个字符串按照格式输出就可以了,很水 #include <stdio.h> ...
- LPC LINK2 IO CONNECTOR
- Using an LPC-Link2 as an LPC4370 evaluation board
https://www.lpcware.com/content/faq/lpcxpresso/using-lpclink2-as-lpc4370-eval As well as being a sta ...
- Linux网络编程socket选项之SO_LINGER,SO_REUSEADDR
from http://blog.csdn.net/feiyinzilgd/article/details/5894300 Linux网络编程中,socket的选项很多.其中几个比较重要的选项有:SO ...
- C#编程(五十八)----------并行集合
并行集合 对于并行任务,与其相关紧密的就是对一些共享资源,数据结构的并行访问.经常要做的就是对一些队列进行加锁-解锁,然后执行类似插入,删除等等互斥操作. .NET4提供了一些封装好的支持并行操作数据 ...
- Java加密技术(一)——加密介绍
from://http://blog.csdn.net/janronehoo/article/details/7590772 如基本的单向加密算法: BASE64 严格地说,属于编码格式,而非加密算法 ...
- Windows:C:\Windows\System32\drivers\etc\hosts
原文地址:http://zhumeng8337797.blog.163.com/blog/static/100768914201001983953188/. host是一个没有扩展名的系统文件,可以用 ...
- java解压缩zip和rar的工具类
package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...
- 清道夫第一季/全集Ray Donovan迅雷下载
清道夫 第一季 Ray Donovan Season 1 (2013)本季看点:Ray Donovan.一位专职于为洛杉矶的名人和富豪服务的神秘人士.他可以巧妙的解决这个城市中富豪们的那些最麻烦同时又 ...