6

Unity3D研究院之Unity5.6的Lz4打ipa、apk包(九十二)

 3 years ago
source link: https://www.xuanyusong.com/archives/4324
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Unity3D研究院之Unity5.6的Lz4打ipa、apk包(九十二)

Unity5.6今天发布了。支持打包lz4压缩包,支持ios和android。 这个lz4不是assetbundle,而是压缩预制在游戏包内的资源。比如场景依赖的资源,或者Resources目录下的资源,Prefab关联的资源等等。

压缩资源的描述如下

Compressed data files include:
1. Player settings – globalgamemanagers and globalgamemanagers.assets files.
2. Scenes and Assets – level* and sharedassets*.asset files.
3. Resources – resources.assets files.
4. GI data.
5. Built-in resources – unity_builtin_extra file.

之前Unity默认打的包都是zip也就是lzma 格式。因为压缩的比较大,那么解压就会慢。用新版lz4打包的好处就是游戏包可能大一点,但是解压的速度就更快。换句话说实例化gameObject 到Hierarchy的时候就会快。

我在小米Pad真机上测试了一下。

为了能突出时间,我拷贝了N张图片关联在Prefab上。在真机上来看加载的时间。

Unity3D研究院之Unity5.6的Lz4打ipa、apk包(九十二) - 雨松MOMO程序研究院 - 1

接着是打包的代码,因为我要同时出两个包,所以这里打了个lz4的和一个默认zip的包。

[MenuItem("build/build")]
static void Start () {
PlayerSettings.bundleIdentifier = "com.yusongmomo.lz4";
BuildPipeline.BuildPlayer (
EditorBuildSettings.scenes, "export_lz4.apk",
BuildTarget.Android,
BuildOptions.CompressWithLz4 | BuildOptions.ConnectWithProfiler );
PlayerSettings.bundleIdentifier = "com.yusongmomo.none";
BuildPipeline.BuildPlayer (
EditorBuildSettings.scenes, "export_none.apk",
BuildTarget.Android,
BuildOptions.ConnectWithProfiler);

场景里把下面的脚本挂上去,把Resources.load的时间打印在屏幕上。。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
using System.Diagnostics;
public class NewBehaviourScript : MonoBehaviour {
string total =string.Empty;
// Use this for initialization
void Start () {
Stopwatch sw = new Stopwatch();
sw.Start();
GameObject.Instantiate<GameObject> (Resources.Load<GameObject> ("1"));
sw.Stop();
total = string.Format("total: {0} ms",sw.ElapsedMilliseconds);
void OnGUI ()
GUI.skin.label.fontSize=50;
GUILayout.Label (total);

APK打出来后,可以看到zip的压缩格式比lzm4小一点。

把这两个包都装在手机上分别看看加载的速度吧。

请看左上角的加载速度,先看看zip的包。

在看看的lz4的包,加载速度完胜zip。。

最后总结一下:

如果可以容忍游戏包大一点,我觉得用lz4打包还是很给力的。

如果你的游戏包大量使用assetbundle,可能优势就不是那么明显。不过初始包肯定还是包含资源的,那么这些资源用lz4还是会快。

作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK