`

Unityios开发--Unity bundle的制作和使用

 
阅读更多

Unity有个很好的功能,大致是很多专注于PC的engine没有提供的(因为没有必要),就是能加载主包外的资源,这个主包外的资源,unity把它叫做bundle。这个功能的提供,主要是在web上或者是ios、android等设备上,主包太大的话,会遇上下载主包时间太长,体验不佳;或者是有些市场直接就限制了主包的大小。

制作的bundle的方式,这里只讲bundle中连scene都带入的:

1、Assets/Editor下面放入编辑器使用的菜单代码,代码如下:

o<wbr><wbr><wbr><span></span>@MenuItem("Tools/Build<wbr>Streamed<wbr>Asset<wbr>Bundle")</wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>static<wbr>function<wbr>ExportResource<wbr>()<wbr>{</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>var<wbr>path<wbr>=<wbr>EditorUtility.SaveFilePanel<wbr>("Build<wbr>Bundle",<wbr>"",<wbr>"*",<wbr>"unity3d");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>if<wbr>(path.Length<wbr>!=<wbr>0)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>{</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var<wbr>levels<wbr>:<wbr>String[]<wbr>=<wbr>["Assets/mybundlelevel.unity"];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>BuildPipeline.BuildStreamedSceneAssetB<wbr>undle<wbr>(levels,<wbr>path,<wbr>BuildTarget.Android);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>}</wbr></wbr></wbr>

[javascript]<wbr>view plaincopy</wbr>

o<wbr><wbr><wbr><span></span>@MenuItem("Tools/Build<wbr>Streamed<wbr>Asset<wbr>Bundle")</wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>static<wbr>function<wbr>ExportResource<wbr>()<wbr>{</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>var<wbr>path<wbr>=<wbr>EditorUtility.SaveFilePanel<wbr>("Build<wbr>Bundle",<wbr>"",<wbr>"*",<wbr>"unity3d");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>if<wbr>(path.Length<wbr>!=<wbr>0)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>{</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>var<wbr>levels<wbr>:<wbr>String[]<wbr>=<wbr>["Assets/mybundlelevel.unity"];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>BuildPipeline.BuildStreamedSceneAssetB<wbr>undle<wbr>(levels,<wbr>path,<wbr>BuildTarget.Android);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>}</wbr></wbr></wbr>

上面的代码的最后一行,最后一个参数,你可以用需要的平台填入。

2、上面的代码放入以后,在Unity的Tools目录下,可以找到“Build Streamed Asset Bundle”。点击这个菜单,就可以为mybundlelevel.unity这个scene生成相关的.unity3d文件,也就是我们需要的bundle文件。

这样制作bundle的过程就结束了。你可以在主包中删除这个关卡而节省主包的空间。注意的是,你可以删除这个关卡需要的所有的资源,只不过要注意不要删除脚本文件、shader文件,还有就是可能被其他关卡引用到的文件。

使用bundle的方式:

在你的程序中,用下面的代码读取和装载相关的bundle

o<wbr><wbr><wbr><span></span>WWW<wbr>download<wbr>=<wbr>WWW.LoadFromCacheOrDownload("file://"<wbr>+<wbr>dataPath<wbr>+<wbr>"mybundlelevel.unity3d",<wbr>1);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>if<wbr>(download.error<wbr>!=<wbr>null)</wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>{</wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>Debug.LogError(download.error);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>return<wbr>false;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>}</wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>AssetBundle<wbr>bundle<wbr>=<wbr>download.assetBundle;</wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>WWW<wbr>download<wbr>=<wbr>WWW.LoadFromCacheOrDownload("file://"<wbr>+<wbr>dataPath<wbr>+<wbr>"mybundlelevel.unity3d",<wbr>1);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>if<wbr>(download.error<wbr>!=<wbr>null)</wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>{</wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>Debug.LogError(download.error);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span><wbr><wbr><wbr><wbr>return<wbr>false;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>}</wbr></wbr></wbr>

o<wbr><wbr><wbr><span></span>AssetBundle<wbr>bundle<wbr>=<wbr>download.assetBundle;</wbr></wbr></wbr></wbr></wbr></wbr>

注意最后一句代码并非想象中的没有必要(特别是C和C++程序员看到这个语句可能是某个function的最后一句),Unity文档说提到,这样写过后,资源才会被真正load进来。

LoadFromCacheOrDownload()的第一个参数,是一个URI,用"file://"开头,说明是本地的文件。用"http://“开头,说明文件在网络上。

上面的代码正确无误的跑过后,你就可以当做原来的level已经在你的主包中了。现在就可以用Application.LoadLevel()来装载这个关卡。

上述方式笔者在Android平台上测试,完全没有任何问题。而且加载速度也很快,在Unity论坛上,有老外遇上加载的速度很慢,笔者没有遇到。

另,这个过程可能遇上让你抓头的问题:

Application.LoadLevel()时,Unity报错,大致是“xxx<wbr>couldn't be loaded because it<wbr>has not been added to the build settings”。这个问题的产生,你可以检查以下几个地方:</wbr></wbr>

1、生成bundle的时候,bundle生成没有错误;

2、bundle通过一定的方式放到了正确的路径;

3、LoadFromCacheOrDownload()的参数一,用了file://或者是http://开头;

4、可以更改LoadFromCacheOrDownload()的第二个参数试试;

不需要考虑的是,在bundle中的scene(level),是不需要在BuildSetting中勾上的,也没有办法勾,因为被你删除了,对吧!Unity的这个错误提示容易让人想入非非。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics