原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0114/333.html
常州手游App开发-U3D手游资源打包Assetbundle
	
	在U3D手游的运营过程中,更新资源是比不可少的。资源管理第一步是资源打包。传统的打包可以将所有物件制成预设Prefab,打包成场景。今天我们来一起学习常州微信公众平台官方推荐的Assetbundle,它是Unity(Pro)提供的资源打包策略。利用AssetBundle,可以将几乎所有的资源都打包封装,便于客户端更新下载新的资源。
- 创建AssetBundle
 
2.新建一个脚本ExportAssetBundles.cs(代码来自官方文档),保存在Asset/Editor目录下
- //在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盘)
	 
- [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,包括加载普通常州手机App开发asset和场景。
	 
- 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依赖关系
 
- //启用交叉引用,用于所有跟随的资源包文件,直到我们调用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();
 
- 总结
 
这一节的内容偏实际操作,手机App外包官方文档和雨松的blog中已经有更加详细介绍。如果大家还有不明白的地方,可以结合文档再实际操作一下。后面的章节,我将花更多的时间介绍核心的内容:资源的增量更新,和代码程序的更新。
上篇:上一篇:常州手游开发设计-U3D占用内存太大解决
下篇:下一篇:常州微信公众号游戏开发-U3D读取XML文件里的属性




