Unity手游之路<十一>资源打包Assetbundle
2014-06-25 17:25
417 查看
在手游的运营过程中,更新资源是比不可少的。资源管理第一步是资源打包。传统的打包可以将所有物件制成预设Prefab,打包成场景。今天我们来一起学习官方推荐的Assetbundle,它是Unity(Pro)提供的资源打包策略。利用AssetBundle,可以将几乎所有的资源都打包封装,便于客户端更新下载新的资源。
(转载请注明原文出处/article/1588728.html)
创建AssetBundle
1.创建一个空的Prefab,命名Cube,然后创建一个Cube,将其拉到刚创建好的Prefab
2.新建一个脚本ExportAssetBundles.cs(代码来自官方文档),保存在Asset/Editor目录下
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
//在Unity编辑器中添加菜单
[MenuItem("Assets/Build AssetBundle From Selection")]
static void ExportResourceRGB2()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource",
"", "New Resource",
"assetbundle");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//打包
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
}
}
3.选中预设Cube,运行Build AssetBundle From Selection。这时会弹出一个保存框,将其命名为cube.unity3d(这里为了测试方便,放在c盘。实际项目中,我们是需要将他们放在web服务器,供所有客户端下载更新)
4.新建一个场景scene1.unity,上面放置几个模型,然后保存
5.选中该场景,在之前的ExportAssetBundles.cs脚本中添加打包场景的函数,运行Assets->Build Scene,保存为scene1.unity3d(这里为了测试方便,也放在c盘)
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
[MenuItem("Assets/Save Scene")]
static void ExportScene()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource",
"", "New Resource",
"unity3d");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
string[] scenes = {"Assets/scene1.unity"};
//打包
BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
}
}
注意事项
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
using System;
using UnityEngine;
using System.Collections;
public class Load: MonoBehaviour
{
private string BundleURL =
"file:///C:/cube.assetbundle";
private string SceneURL =
"file:///C:/scene1.unity3d";
void Start()
{
//BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";
Debug.Log(BundleURL);
StartCoroutine(DownloadAssetAndScene());
}
IEnumerator DownloadAssetAndScene()
{
//下载assetbundle,加载Cube
using (WWW asset = new WWW(BundleURL))
{
yield return asset;
AssetBundle bundle = asset.assetBundle;
Instantiate(bundle.Load("Cube"));
bundle.Unload(false);
yield return new WaitForSeconds(5);
}
//下载场景,加载场景
using (WWW scene =
new WWW(SceneURL))
{
yield return scene;
AssetBundle bundle = scene.assetBundle;
Application.LoadLevel("scene1");
}
}
}
注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
AssetBundle依赖关系
如果一个公共对象被多个对象依赖,我们打包的时候,可以有两种选取。一种是比较省事的,就是将这个公共对象打包到每个对象中。这样会有很多弊端:内存被浪费了;加入公共对象改变了,每个依赖对象都得重新打包。AssetBundle提供了依赖关系打包。我们通过一个简单的例子来学习
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
//启用交叉引用,用于所有跟随的资源包文件,直到我们调用PopAssetDependencies
BuildPipeline.PushAssetDependencies();
var options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets;
//所有后续资源将共享这一资源包中的内容,由你来确保共享的资源包是否在其他资源载入之前载入
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),
null, "Shared.unity3d", options);
//这个文件将共享这些资源,但是后续的资源包将无法继续共享它
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),
null, "Lerpz.unity3d", options);
BuildPipeline.PopAssetDependencies();
这个文件将共享这些资源,但是后续的资源包将无法继续共享它
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),
null, "explosive.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
总结
这一节的内容偏实际操作,官方文档和雨松的blog中已经有更加详细介绍。如果大家还有不明白的地方,可以结合文档再实际操作一下。后面的章节,我将花更多的时间介绍核心的内容:资源的增量更新,和代码程序的更新。
源码
http://pan.baidu.com/s/1i3BOAPn
参考资料
1.http://www.xuanyusong.com/
2.http://game.ceeger.com/Manual/DownloadingAssetBundles.html
(转载请注明原文出处/article/1588728.html)
创建AssetBundle
1.创建一个空的Prefab,命名Cube,然后创建一个Cube,将其拉到刚创建好的Prefab
2.新建一个脚本ExportAssetBundles.cs(代码来自官方文档),保存在Asset/Editor目录下
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
//在Unity编辑器中添加菜单
[MenuItem("Assets/Build AssetBundle From Selection")]
static void ExportResourceRGB2()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource",
"", "New Resource",
"assetbundle");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//打包
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
}
}
//在Unity编辑器中添加菜单 [MenuItem("Assets/Build AssetBundle From Selection")] static void ExportResourceRGB2() { // 打开保存面板,获得用户选择的路径 string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle"); if (path.Length != 0) { // 选择的要保存的对象 Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); //打包 BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows); } }这时我们将看到Asset下面出现Build AssetBundle From Selection和Build Scene
3.选中预设Cube,运行Build AssetBundle From Selection。这时会弹出一个保存框,将其命名为cube.unity3d(这里为了测试方便,放在c盘。实际项目中,我们是需要将他们放在web服务器,供所有客户端下载更新)
4.新建一个场景scene1.unity,上面放置几个模型,然后保存
5.选中该场景,在之前的ExportAssetBundles.cs脚本中添加打包场景的函数,运行Assets->Build Scene,保存为scene1.unity3d(这里为了测试方便,也放在c盘)
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
[MenuItem("Assets/Save Scene")]
static void ExportScene()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource",
"", "New Resource",
"unity3d");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
string[] scenes = {"Assets/scene1.unity"};
//打包
BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
}
}
[MenuItem("Assets/Save Scene")] static void ExportScene() { // 打开保存面板,获得用户选择的路径 string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d"); if (path.Length != 0) { // 选择的要保存的对象 Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); string[] scenes = {"Assets/scene1.unity"}; //打包 BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes); } }
注意事项
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
using System;
using UnityEngine;
using System.Collections;
public class Load: MonoBehaviour
{
private string BundleURL =
"file:///C:/cube.assetbundle";
private string SceneURL =
"file:///C:/scene1.unity3d";
void Start()
{
//BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";
Debug.Log(BundleURL);
StartCoroutine(DownloadAssetAndScene());
}
IEnumerator DownloadAssetAndScene()
{
//下载assetbundle,加载Cube
using (WWW asset = new WWW(BundleURL))
{
yield return asset;
AssetBundle bundle = asset.assetBundle;
Instantiate(bundle.Load("Cube"));
bundle.Unload(false);
yield return new WaitForSeconds(5);
}
//下载场景,加载场景
using (WWW scene =
new WWW(SceneURL))
{
yield return scene;
AssetBundle bundle = scene.assetBundle;
Application.LoadLevel("scene1");
}
}
}
using System; using UnityEngine; using System.Collections; public class Load: MonoBehaviour { private string BundleURL = "file:///C:/cube.assetbundle"; private string SceneURL = "file:///C:/scene1.unity3d"; void Start() { //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle"; Debug.Log(BundleURL); StartCoroutine(DownloadAssetAndScene()); } IEnumerator DownloadAssetAndScene() { //下载assetbundle,加载Cube using (WWW asset = new WWW(BundleURL)) { yield return asset; AssetBundle bundle = asset.assetBundle; Instantiate(bundle.Load("Cube")); bundle.Unload(false); yield return new WaitForSeconds(5); } //下载场景,加载场景 using (WWW scene = new WWW(SceneURL)) { yield return scene; AssetBundle bundle = scene.assetBundle; Application.LoadLevel("scene1"); } } }
注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
AssetBundle依赖关系
如果一个公共对象被多个对象依赖,我们打包的时候,可以有两种选取。一种是比较省事的,就是将这个公共对象打包到每个对象中。这样会有很多弊端:内存被浪费了;加入公共对象改变了,每个依赖对象都得重新打包。AssetBundle提供了依赖关系打包。我们通过一个简单的例子来学习
[csharp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
//启用交叉引用,用于所有跟随的资源包文件,直到我们调用PopAssetDependencies
BuildPipeline.PushAssetDependencies();
var options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets;
//所有后续资源将共享这一资源包中的内容,由你来确保共享的资源包是否在其他资源载入之前载入
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),
null, "Shared.unity3d", options);
//这个文件将共享这些资源,但是后续的资源包将无法继续共享它
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),
null, "Lerpz.unity3d", options);
BuildPipeline.PopAssetDependencies();
这个文件将共享这些资源,但是后续的资源包将无法继续共享它
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(
AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),
null, "explosive.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
//启用交叉引用,用于所有跟随的资源包文件,直到我们调用PopAssetDependencies BuildPipeline.PushAssetDependencies(); var options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets; //所有后续资源将共享这一资源包中的内容,由你来确保共享的资源包是否在其他资源载入之前载入 BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"), null, "Shared.unity3d", options); //这个文件将共享这些资源,但是后续的资源包将无法继续共享它 BuildPipeline.PushAssetDependencies(); BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"), null, "Lerpz.unity3d", options); BuildPipeline.PopAssetDependencies(); 这个文件将共享这些资源,但是后续的资源包将无法继续共享它 BuildPipeline.PushAssetDependencies(); BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"), null, "explosive.unity3d", options); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies();我们在程序加载的时候必须保证先加载公共对象。否则,只能是在各个对象加载成功后,再通过程序手动添加进来,比较繁琐。在实际项目中,由于是团队开发,对象间的依赖关系通常会比较凌乱,最好在开发周期就定好相关的规范约束,方便管理。
总结
这一节的内容偏实际操作,官方文档和雨松的blog中已经有更加详细介绍。如果大家还有不明白的地方,可以结合文档再实际操作一下。后面的章节,我将花更多的时间介绍核心的内容:资源的增量更新,和代码程序的更新。
源码
http://pan.baidu.com/s/1i3BOAPn
参考资料
1.http://www.xuanyusong.com/
2.http://game.ceeger.com/Manual/DownloadingAssetBundles.html
相关文章推荐
- Unity手游之路<十一>资源打包Assetbundle(转)
- Unity手游之路<十一>资源打包Assetbundle
- Unity手游之路<十一>资源打包Assetbundle
- Unity手游之路<十一>资源打包Assetbundle
- [Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题
- AssetBundle系列——共享资源打包/依赖资源打包
- (转)AssetBundle系列——游戏资源打包(二)
- Unity资源打包之Assetbundle
- unity5.0资源打包AssetBundle
- Unity3d之AssetBundle打包与服务器资源下载
- Unity3D中实现按资源名称自动化命名打包AssetBundle
- Unity3D之AssetBundle【7】共享资源打包/依赖资源打包
- Unity资源打包之Assetbundle
- Unity3d的AssetBundle打包——AssetBundle Browser(简单实现资源复用)
- AssetBundle系列——场景资源之打包(一)
- 资源打包Assetbundle .
- Unity3d的AssetBundle打包——AssetBundle Browser(简单实现资源复用)
- AssetBundle系列——游戏资源打包(一)
- Unity资源打包之Assetbundle
- Unity资源打包之Assetbundle