Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱黑铅笔的猫
minigame-unity-webgl-transform
提交
4785c9af
M
minigame-unity-webgl-transform
项目概览
爱黑铅笔的猫
/
minigame-unity-webgl-transform
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
minigame-unity-webgl-transform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4785c9af
编写于
6月 23, 2021
作者:
J
junkunzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: assetbundle使用介绍
上级
aabed2da
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
123 addition
and
0 deletion
+123
-0
Design/UsingAssetBundle.md
Design/UsingAssetBundle.md
+123
-0
image/assetbundle/assetbundle-scene.png
image/assetbundle/assetbundle-scene.png
+0
-0
image/assetbundle/assetbundle-unnormal-memory.png
image/assetbundle/assetbundle-unnormal-memory.png
+0
-0
image/assetbundle/normal-assetbundle-memory.png
image/assetbundle/normal-assetbundle-memory.png
+0
-0
image/assetbundle/set-bundlename.png
image/assetbundle/set-bundlename.png
+0
-0
未找到文件。
Design/UsingAssetBundle.md
0 → 100644
浏览文件 @
4785c9af
# 使用AssetBundle
*首先,相比AssetBundle,更推荐使用[Addressable Assets System](UsingAddressable.md)*
本文对AssetBundle使用做简要介绍,同时分析四种下载方式的内存占用情况
## 一、AssetBundle使用
注意:小游戏环境不支持assetbundle本地加载
### 1.1 组织资源
自行组织资源如何分组,以及各个bundle之间依赖关系,包名相同的资源会打包到一起
<image
src=
"../image/assetbundle/set-bundlename.png"
width=
"600"
/>
### 1.2 AssetBundle打包
```
c#
public
static
void
Build
()
{
string
dst
=
Application
.
streamingAssetsPath
+
"/AssetBundles"
;
if
(!
Directory
.
Exists
(
dst
))
{
Directory
.
CreateDirectory
(
dst
);
}
BuildPipeline
.
BuildAssetBundles
(
dst
,
BuildAssetBundleOptions
.
AppendHashToAssetBundleName
|
BuildAssetBundleOptions
.
ChunkBasedCompression
|
BuildAssetBundleOptions
.
None
,
BuildTarget
.
WebGL
);
}
```
打包bundle时,请使用如下参数
-
【重要】BuildAssetBundleOptions.AppendHashToAssetBundleName:bundle带上hash。在小游戏底层对bundle做缓存及缓存淘汰时,hash是重要依据。
-
BuildAssetBundleOptions.ChunkBasedCompression:LZ4压缩方式,加载速度和包体大小更均衡。
### 1.3 AssetBundle下载
从服务器下载bundle的方式主要以下四种。
**注意:WWW和WWW.LoadFromCacheOrDownload已不被Unity推荐使用**
-
UnityWebRequestAssetBundle.GetAssetBundle
```
c#
UnityWebRequest
request
=
UnityWebRequestAssetBundle
.
GetAssetBundle
(
uriPath
);
yield
return
request
.
SendWebRequest
();
if
(
request
.
isHttpError
)
{
Debug
.
LogError
(
GetType
()
+
"/ERROR/"
+
request
.
error
);
}
else
{
AssetBundle
ab
=
(
request
.
downloadHandler
as
DownloadHandlerAssetBundle
).
assetBundle
;
}
```
-
UnityWebRequest
```
c#
UnityWebRequest
www
=
new
UnityWebRequest
(
uriPath
);
DownloadHandlerAssetBundle
handler
=
new
DownloadHandlerAssetBundle
(
www
.
uri
.
ToString
(),
0
);
www
.
downloadHandler
=
handler
;
yield
return
www
.
Send
();
if
(
www
.
isHttpError
)
{
Debug
.
LogError
(
GetType
()
+
"/ERROR/"
+
www
.
error
);
}
else
{
AssetBundle
ab
=
handler
.
assetBundle
}
```
-
WWW.LoadFromCacheOrDownload
```
c#
WWW
www
=
WWW
.
LoadFromCacheOrDownload
(
uriPath
,
2
);
yield
return
www
;
if
(
www
.
error
!=
null
)
{
Debug
.
Log
(
www
.
error
);
yield
break
;
}
AssetBundle
ab
=
www
.
assetBundle
;
```
-
WWW
```
c#
WWW
www
=
new
WWW
(
uriPath
);
yield
return
www
;
if
(
www
.
error
!=
null
)
{
Debug
.
Log
(
www
.
error
);
yield
break
;
}
AssetBundle
ab
=
www
.
assetBundle
;
```
界面如下
<image
src=
"../image/assetbundle/assetbundle-scene.png"
width=
"600"
/>
按钮从上往下加载方式分别为使用
`UnityWebRequestAssetBundle.GetAssetBundle`
,
`UnityWebRequest`
,
`WWW.LoadFromCacheOrDownload`
,
`WWW`
。
### 1.4 AssetBundle内资源加载
下载完毕后,加载bundle内资源可使用
`AssetBundle.LoadAsset`
代码示意如下
```
c#
StartCoroutine
(
UnityWebRequestLoad
(
string
.
Format
(
"http://127.0.0.1:8080/StreamingAssets/AssetBundles/{0}"
,
bundleName
),
(
ab
)
=>
{
Sprite
img
=
ab
.
LoadAsset
<
Sprite
>(
"deer"
);
image
.
GetComponent
<
SpriteRenderer
>().
sprite
=
img
;
ab1
=
ab
;
img1Loaded
=
true
;
}));
```
## 二、AssetBundle下载API内存分析
加载的bundle文件大小为5403162字节
```
bash
-rw-r--r--
1 junkunzhang TENCENT
\D
omain Users 5403162 6 22 16:07 image1_751427a66d22b184c95342d556354150
```
通过加载前后snapshot来对比总内存变化
-
UnityWebRequestAssetBundle.GetAssetBundle/UnityWebRequest/WWW
这三种加载方式加载前后内存无变化
<image
src=
"../image/assetbundle/normal-assetbundle-memory.png"
/>
-
WWW.LoadFromCacheOrDownload
加载bundle后内存增加了5403162字节,增加了bundle大小的内存,是因为WWW.LoadFromCacheOrDownload除了会将文件存入IndexDB外,还会存入内存中的UnityCache
<image
src=
"../image/assetbundle/assetbundle-unnormal-memory.png"
/>
**因此需要注意业务中不要使用已淘汰的WWW类,尤其WWW.LoadFromCacheOrDownload,当bundle数量多时,会浪费不少内存。**
## 三、参考资料
1.
Introduction to Asset Bundles
https://learn.unity.com/tutorial/introduction-to-asset-bundles#6028bab6edbc2a750bf5b8a7
2.
Unity WebGL Memory Optimization: Part Deux
https://blog.kongregate.com/unity-webgl-memory-optimization-part-deux/
3.
Understanding Memory in Unity WebGL
https://blog.unity.com/technology/understanding-memory-in-unity-webgl
4.
AssetBundle API
https://docs.unity3d.com/2019.4/Documentation/ScriptReference/AssetBundle.html
\ No newline at end of file
image/assetbundle/assetbundle-scene.png
0 → 100644
浏览文件 @
4785c9af
53.3 KB
image/assetbundle/assetbundle-unnormal-memory.png
0 → 100644
浏览文件 @
4785c9af
548.5 KB
image/assetbundle/normal-assetbundle-memory.png
0 → 100644
浏览文件 @
4785c9af
406.1 KB
image/assetbundle/set-bundlename.png
0 → 100644
浏览文件 @
4785c9af
234.3 KB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录