好处(Lua代码热更新):
1、迅速修复Bug,避免重新下载安装包
2、提升玩家留存率,避免玩家因为超大安装包而流失
3、迅速换”内核“,替换游戏逻辑
热更新的基础:AB包。支持压缩,可以减少包体大小,灵活,不用固定文件路径,存放资源集合。
流程(AB包):
1.版本号比较,如果版本号不同才继续下面的流程(可选,也可以直接下载)
2.下载对比文件,下载资源服务器上的的对比文件
3.对比,用下载的文件和本地文件对比,记录改变的和新增的
4.下载资源,下载刚才记录的文件——一般放在Application.persistentDataPath
5.解压(可选)——如果文件是压缩过的,要先解压
6.覆盖本地对比文件;保证下载成功后 将下载的对比文件 覆盖本地的
使用Asset Bundler加载资源:
需要先将AB包打包并复制到到Application.streamingAssetsPath,以便加载。
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ABTest : MonoBehaviour
{
[SerializeField] private Image testImage;
void Start()
{
//加载AB包,同名AB包不能重新加载,会报错
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/model");
//加载AB包的资源,指定资源类型
GameObject obj = ab.LoadAsset("Cube", typeof(GameObject)) as GameObject;
Instantiate(obj);
//如传入True则卸载所有用AB加载的物体,否则只卸载AB包
AssetBundle.UnloadAllAssetBundles(false);
//卸载指定AB包
//ab.Unload(false);
ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/model");
GameObject obj2 = ab.LoadAsset("Sphere", typeof(GameObject)) as GameObject;
Instantiate(obj2);
StartCoroutine(LoadABRes("icon", "Angry"));
}
/// <summary>
/// 异步加载AB包资源
/// </summary>
/// <param name="ABName"></param>
/// <param name="resName"></param>
/// <returns></returns>
IEnumerator LoadABRes(string ABName, string resName)
{
AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/" + ABName);
yield return abcr;
AssetBundleRequest abr = abcr.assetBundle.LoadAssetAsync(resName, typeof(Sprite)) ;
yield return abr;
Sprite sprite = abr.asset as Sprite;
testImage.sprite = sprite;
yield return null;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
AssetBundle.UnloadAllAssetBundles(true);
}
}
评论(0)
暂无评论