Unity3D研究院之Assetbundle的实战
上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一)本篇文章我们将说说assetbundle是如何实现的。
1.创建Assetbundle
无论是模型资源还是UI资源,最好是先把他们放在Prefab中,然后在做成Assetbundle。我们以模型来举例,Assetbundle中可以放一个模型、也可以放多个模型,它是非常灵活了那么最需要考虑的就是模型空间占用的问题。
比如我们有两个完全一样的模型,但是他们身上绑定的脚本不一样,此时需要把这两个模型放在两个不同Prefab中。如下图所示,我们分别对这两个Prefab打包,我们可以清晰的看到两个相同的Prefab打包在一起只占1M空间,而将他们分别打包会占1 + 1 = 2M空间。 Prefab在打包的同时会把模型身上的所有材质、贴图、组件、脚本全部包含进去。
由此可得相同的模型尽量打包在一起,他们会公用一套资源文件。不相同的模型尽量分开打包,相同模型具有不同的脚本、组件的话把他们放在不同的Prefab中,最后把这些Prefab一起打包在一个Assetbundle中。如下图所示,现在Project视图中选择需要打包的Prefab,然后在导航菜单栏中选择Create Assetbundles Main表示分别打包、Create AssetBundles All表示将他们打包在一起。
这两个prefab文件都指向了同一个模型,为了让它俩有所区别,我给它俩绑定了相同的脚本,但是脚本中的参数是不同的。在编辑器上给每个Prefab赋值一个不同的名子,然后在Awake方法中进行输出。
01
02
03
04
05
06
07
08
09
10
11
12
|
using UnityEngine;
using System.Collections;
public class Script : MonoBehaviour
{ public string name ;
void Awake ( )
{
Debug.Log ( "my name is " + name ) ;
}
} |
Create Assetbundles Main : 分开打包,会生成两个Assetbundle。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[MenuItem ( "Custom Editor/Create AssetBunldes Main" ) ]
static void CreateAssetBunldesMain ( )
{ / / 获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered ( typeof ( Object ) , SelectionMode.DeepAssets ) ;
/ / 遍历所有的游戏对象
foreach ( Object obj in SelectedAsset )
{
string sourcePath = AssetDatabase.GetAssetPath ( obj ) ;
/ / 本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
/ / StreamingAssets是只读路径,不能写入
/ / 服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj. name + ".assetbundle" ;
if ( BuildPipeline.BuildAssetBundle ( obj , null , targetPath , BuildAssetBundleOptions.CollectDependencies ) ) {
Debug.Log ( obj. name + "资源打包成功" ) ;
}
else
{
Debug.Log ( obj. name + "资源打包失败" ) ;
}
}
/ / 刷新编辑器
AssetDatabase.Refresh ( ) ;
} |
最核心的方法其实就它:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)
参数1:它只能放一个对象,因为我们这里是分别打包,所以通过循环将每个对象分别放在了这里。
参数2:可以放入一个数组对象。
默认情况下打的包只能在电脑上用,如果要在手机上用就要添加一个参数。
上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)
IOS上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)
另外,电脑上和手机上打出来的Assetbundle不能混用,不同平台只能用自己的。
Create AssetBundles All:将所有对象打包在一个Assetbundle中。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
< font face = "Verdana," > [MenuItem ( "Custom Editor/Create AssetBunldes ALL" ) ]
static void CreateAssetBunldesALL ( )
{
Caching.CleanCache ( ) ;
string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle" ;
Object[] SelectedAsset = Selection.GetFiltered ( typeof ( Object ) , SelectionMode.DeepAssets ) ;
foreach ( Object obj in SelectedAsset )
{
Debug.Log ( "Create AssetBunldes name :" + obj ) ;
}
/ / 这里注意第二个参数就行
if ( BuildPipeline.BuildAssetBundle ( null , SelectedAsset , Path , BuildAssetBundleOptions.CollectDependencies ) ) {
AssetDatabase.Refresh ( ) ;
} else {
}
} < / font >
|
两次打包完毕后,在StreamingAssets文件夹中就看到了这三个assetbundle文件。
2.读取Assetbundle
然后我们来学习如何运行时读取Assetbundle,Assetbundle是可以同时放在服务器或者本地的,无论放在哪里两种下载读取的方式是完全一样的。所以我建议在做unity项目的时候开始就把资源放在Assetbundle中在本地来做,等做的差不多了直接把Assetbundle放在服务器上,因为两种读取的方式完全一样,这样以后更新资源会方便很多。然后是读取,并且加载到游戏中。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
using UnityEngine;
using System.Collections;
public class RunScript : MonoBehaviour
{ / / 不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID "jar:file://" + Application.dataPath + "!/assets/" ;
#elif UNITY_IPHONE Application.dataPath + "/Raw/" ;
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR "file://" + Application.dataPath + "/StreamingAssets/" ;
#else string .Empty;
#endif void OnGUI ( )
{
if ( GUILayout.Button ( "Main Assetbundle" ) )
{
StartCoroutine ( LoadMainGameObject ( PathURL + "Prefab0.assetbundle" ) ) ;
StartCoroutine ( LoadMainGameObject ( PathURL + "Prefab1.assetbundle" ) ) ;
}
if ( GUILayout.Button ( "ALL Assetbundle" ) )
{
StartCoroutine ( LoadALLGameObject ( PathURL + "ALL.assetbundle" ) ) ;
}
}
/ / 读取一个资源
private IEnumerator LoadMainGameObject ( string path )
{
WWW bundle = new WWW ( path ) ;
yield return bundle ;
/ / 加载到游戏中
yield return Instantiate ( bundle .assetBundle.mainAsset ) ;
bundle .assetBundle.Unload ( false ) ;
}
/ / 读取全部资源
private IEnumerator LoadALLGameObject ( string path )
{
WWW bundle = new WWW ( path ) ;
yield return bundle ;
/ / 通过Prefab的名称把他们都读取出来
Object obj 0 = bundle .assetBundle.Load ( "Prefab0" ) ;
Object obj 1 = bundle .assetBundle.Load ( "Prefab1" ) ;
/ / 加载到游戏中
yield return Instantiate ( obj 0 ) ;
yield return Instantiate ( obj 1 ) ;
bundle .assetBundle.Unload ( false ) ;
}
} |
这里我们详细的说说 下载类WWW
WWW bundle = new WWW(path);
这样的做法是通过一个路径进行下载(无论是服务器路径还是本地路径下载操作都一样)但是bundle只能保存在内存中,也就是退出游戏在进入还得重新下,很显然在游戏中我们不能使用这种方式。
01
02
03
04
05
06
07
08
09
10
|
< font face = "Verdana," > private IEnumerator LoadMainCacheGameObject ( string path )
{
WWW bundle = WWW.LoadFromCacheOrDownload ( path , 5 ) ;
yield return bundle ;
/ / 加载到游戏中
yield return Instantiate ( bundle .assetBundle.mainAsset ) ;
bundle .assetBundle.Unload ( false ) ;
} < / font >
|
使用的方法是WWW.LoadFromCacheOrDownload(path,5);参数1:服务器或者本地下载地址参数2:版本号 Unity会下载Assetbundle本地中,它的工作原理是先通过(版本号和下载地址)先在本地去找看有没有这个Assetbundle,如果有直接返回对象,如果没有的话,在根据这个下载地址重新从服务器或者本地下载。这里版本号起到了很重要的作用,举个例子,同一下载地址版本号为1的时候已经下载到本地,此时将版本号的参数改成2 那么它又会重新下载,如果还保持版本号为1那么它会从本地读取,因为本地已经有版本号为1的这个Assetbundle了。你不用担心你的资源本地下载过多,也不用自己手动删除他们,这一切的一切Unity会帮我们自动完成,它会自动删除掉下载后最不常用的Assetbundle ,如果下次需要使用的话只要提供下载地址和版本后它会重新下载。 我们在聊聊Assetbundle 中的脚本,在移动平台下Assetbundle里面放的脚本是不会被执行的,还记得我们打包前给两个Prefab挂上了脚本吗?在手机上将Assetbundle下载到本地后,加载进游戏中Prefab会自动在本地找它身上挂着的脚本,他是根据脚本的名来寻找,如果本地有这条脚本的话,Prefab会把这个脚本重新绑定在自身,并且会把打包前的参数传递进来。如果本地没有,身上挂的条脚本永远都不会被执行。 在Prefab打包前,我在编辑器上给脚本中的变量 name 赋了不同值,当Prefab重新载入游戏的时候,它身上脚本的参数也会重新输出。
如果你的Assetbundle中的Prefab上引用的对象,那么这样做就会出错了,你需要设定他们的依赖关系。或者运行时通过脚本动态的载入对象。、http://docs.unity3d.com/Document … etDependencies.html
http://docs.unity3d.com/Documentation/ScriptReference/BuildPipeline.PushAssetDependencies.html
、像这样重新打包就可以。
3.打包场景
上面我们说过了打包Prefab,其实我们还可以把整个场景进行打包,因为移动平台不能更新脚本,所以这个功能就会有所限制,我的建议是烘培场景、然后把多个场景可复用的对象移除,场景中只保留独一无二的游戏对象,然后在打包场景,运行游戏时载入场景后,在动态的将之前移除的对象重新添加进来。可以参考 : Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)
01
02
03
04
05
06
07
08
09
10
|
< font face = "Verdana," > [MenuItem ( "Custom Editor/Create Scene" ) ]
static void CreateSceneALL ( )
{
/ / 清空一下缓存
Caching.CleanCache ( ) ;
string Path = Application.dataPath + "/MyScene.unity3d" ;
string []levels = { "Assets/Level.unity" } ;
/ / 打包场景
BuildPipeline.BuildPlayer ( levels , Path , BuildTarget.WebPlayer , BuildOptions.BuildAdditionalStreamedScenes ) ;
AssetDatabase.Refresh ( ) ;
} < / font >
|
不同平台下需要选择 BuildTarget.Android 和 BuildTarget.iPhone 。 切记这段代码是把Level.unity常见文件打包到MyScene.unity3d文件中,所以在解包的时候也应当是先解开MyScene.unity3d,然后在去加载Level.unity场景,无需在ProjectSetting中注册新场景。
1
2
3
4
5
6
|
private IEnumerator LoadScene ( )
{ WWW download = WWW.LoadFromCacheOrDownload ( "file://" + Application.dataPath + "/MyScene.unity3d" , 1 ) ;
yield return download;
var bundle = download.assetBundle;
Application.LoadLevel ( "Level" ) ;
} |
在测试情况下你可能会频繁的打包生成Assetbundle,如果忘记改版本号的话可能会读取之前的缓存,可能就会看不到新的效果,所以我建议在bunild Assetbundle的时候强制清空一下缓存。
Caching.CleanCache();
最后点击按钮进行加载Assetbundle和 Scene吧。