1
2
3
4
5
6
7
8
9
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.Networking; public class DownLoad : MonoBehaviour { IEnumerator Start() { //资源包路径 string path1 = "AssetBundles/cubewall.unity3d" ; //共享依赖资源包路径 string path2 = "AssetBundles/share.unity3d" ; //第一种加载AB的方式 ,从内存中加载 LoadFromMemory #region //方法一:异步加载 //加载资源 AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1)); yield return request; //加载共同依赖资源,如贴图、材质 AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2)); yield return request2; AssetBundle ab = request.assetBundle; AssetBundle ab2 = request2.assetBundle; //使用里面的资源 GameObject wallPrefab1 = (GameObject) ab.LoadAsset( "CubeWall" ); Instantiate(wallPrefab1); //方法二:同步加载(无需用协程) //加载资源 AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1)); //加载共同依赖资源,如贴图、材质 AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2)); //使用里面的资源 GameObject wallPrefab2 = (GameObject) ab.LoadAsset( "CubeWall" ); Instantiate(wallPrefab2); #endregion //第二种加载AB的方式 ,从本地加载 LoadFromFile(无需用协程) #region AssetBundle ab5 = AssetBundle.LoadFromFile(path1); AssetBundle ab6 = AssetBundle.LoadFromFile(path2); GameObject wallPrefab3 = (GameObject) ab5.LoadAsset( "CubeWall" ); Instantiate(wallPrefab3); #endregion //第三种加载AB的方式 ,从本地或服务器加载 WWW(将被弃用) #region //是否准备好 while (Caching.ready == false ) { yield return null ; } //从本地加载 //WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1); //从服务器加载 WWW www = WWW.LoadFromCacheOrDownload( @"http://localhost/AssetBundles/cubewall.unity3d" , 1); yield return www; //是否报错 if ( string .IsNullOrEmpty(www.error) == false ) { Debug.Log(www.error); yield break ; } AssetBundle ab7 = www.assetBundle; //使用里面的资源 GameObject wallPrefab4 = (GameObject) ab7.LoadAsset( "CubeWall" ); Instantiate(wallPrefab4); #endregion //第四种加载AB方式 从服务器端下载 UnityWebRequest(新版Unity使用) #region //服务器路径 localhost为IP string uri = @"http://localhost/AssetBundles/cubewall.unity3d" ; UnityWebRequest request3 = UnityWebRequest.GetAssetBundle(uri); yield return request3.Send(); //AssetBundle ab8 = ((DownloadHandlerAssetBundle)request3.downloadHandler).assetBundle; AssetBundle ab8 = DownloadHandlerAssetBundle.GetContent(request3); //使用里面的资源 GameObject wallPrefab5 = (GameObject) ab8.LoadAsset( "CubeWall" ); Instantiate(wallPrefab5); //加载cubewall.unity3d资源包所依赖的资源包 AssetBundle manifestAB = AssetBundle.LoadFromFile( "AssetBundles/AssetBundles" ); AssetBundleManifest manifest = (AssetBundleManifest) manifestAB.LoadAsset( "AssetBundleManifest" ); //foreach(string name in manifest.GetAllAssetBundles()) //{ // print(name); //} //cubewall.unity3d资源包所依赖的资源包的名字 string [] strs = manifest.GetAllDependencies( "cubewall.unity3d" ); foreach ( string name in strs) { AssetBundle.LoadFromFile( "AssetBundles/" + name); } #endregion } } |
什么是AssetBundle
估计很多人只知道Unity的模型之类的东西可以导出成一种叫做AssetBundle的文件,然后打包后可以在Unity程序运行的时候再加载回来用。
那么AssetBundle是一个什么样的东西呢?其实AssetBundle只是一种使用LZMA压缩方式压缩的资源文件。具体LZMA是什么请百度,你可以理解成就是一种压缩文件就行了,至于它的后缀名是什么,一点关系都没有,你可以自己定。
AssetBundle打包的时候,你可以指定一个mainAsset,那么加载完之后就可以通过AssetBundle.mainAsset来获取到了。你也可以不指定mainAsset,直接打包一堆内容进去,然后加载后通过AssetBundle.LoadAsset指定名字的读取出来。
在资源之间,存在着依赖的关系。你可以把资源拆分得很细,比如一个模型,由网格模型、材质、贴图构成,你可以把每一个小部分都拆开,各自打包成压缩文件。当Unity需要加载使用的时候,把该模型的所有依赖的小资源都加载起来,然后根据依赖关系组装,就变回了我们看到的资源了。
详细内容
Includes:
All Settings Pro v1.0.5.unitypackage
Blend Modes v3.4.unitypackage
Crosshair Assembler 1.1.0.unitypackage
DDA Fog 1.02.unitypackage
Distortion Shader Pack 1.3.unitypackage
Exploder 1.7.8.unitypackage
Fantasy Skybox.unitypackage
Lyme 1.0.unitypackage
Mobile City Pack 1.1.unitypackage
Mobile City Pack 1.2.unitypackage
Stylized Forest 1.2.unitypackage
Toon Soldiers v2.0.unitypackage
UniStorm – Dynamic Modular Weather v3.0.1.1.unitypackage
Vistas v3.0.unitypackage
Vistas v4.0.unitypackage
Volumetric Light Beam v1.60.unitypackage
Pixel Platformer Engine – Responsive 2d Platformer Toolkit 1.1.zip
Stylized Low-Poly Nature _et v2.0.zip
Universal WebGL Template 1.2.1.zip
加载方法
大致经历以下过程
WWW
AssetBundle
Asset
WWW
加载:通过url加载
内存大小:
压缩资源:翻倍(为什么会得出翻倍?)
非压缩:几乎相同
内存对象:WebStream
AssetBundle
加载:随着WWW加载,第一次访问的时候会在内存生成一个SerializeFile,内存也会跟着增加0.4M左右(这个0.4M是什么?)
内存大小:在WWW的基础上增加0.4M(非固定)
内存对象:SerializeFile(archive/Cab-XXXX)
Asset
加载:AssetBundle.LoadAsset
相关的脚本,shader,Asset,Transform,GameObject都会被加载进来,存放在内存的Asset下
大小:完整大小,(会对相关的资源进行一次解压)
Unity加载AssetBundle的方法
Unity Asset Bundle 2 – October
链接: https://pan.baidu.com/s/1OQS1DlDBtreCAA4nBOpWeA 提取码: mnqh
Unity Asset Bundle 3 – September 2019
链接: https://pan.baidu.com/s/1ApZkx25phnT96DPtug4WWQ 提取码: q7qj
Unity Asset Bundle 4 – May 2019
链接: https://pan.baidu.com/s/1Jigr5xPRDVFMAZXFeOqfpw 提取码: 1n4s
解压密码 -0daydown
Unity Asset Bundle 5 – July 2019
链接: https://pan.baidu.com/s/1OTIM1iEn8dGy1BsWN9d7eg 提取码: iavc
本文地址:http://www.sd124.com/article/2018/0615/223677.html
《Unity Asset Bundle 2/3/4/5 October 2019 最新版 含加载方法》由闪电下载吧整理并发布,欢迎转载!