1. 基本使用
1. 构建配置文件
1. SplitConfig
创建SplitConfig文件,在xasset目录中选中Settings文件,将创建的SplitConfig文件赋值给对应参数。
2. Group
- 创建Group文件,将需要打包的文件和文件夹拖拽到Entries中
- Filter过滤器将决定哪些文件会被过滤(例:t:Material)
- 设置Bundle Mode,将决定以何种方式构建AB包。
参数 | 描述 |
---|---|
Pack Together | 全部达成一个包 |
Pack By File | 以文件打包 |
Pack By Folder | 以文件夹打包 |
3. Build
创建Build文件,将Group放入Groups菜单中文章来源:https://www.toymoban.com/news/detail-405727.html
4. 其他行为
将xasset目录中的Settings文件,Script Play Mode选项卡设置为Inrement,模拟真实AB包读取。(这会导致每次开始运行时提示将数据迁移到streaming asset)文章来源地址https://www.toymoban.com/news/detail-405727.html
2. 打包文件
- 点击 xasset->Build Bundles 构建AB包
- 点击 xasset->Clear Bundles 清除老的AB包
- 默认构建的包体,将被保存到工程目录下(与Assets同级)
3. 代码编写
- 预加载
using UnityEngine;
using xasset;
using Scene = xasset.Scene;
public class StartGet : MonoBehaviour
{
// Start is called before the first frame update
async void Start()
{
if (!Versions.Initialized)
{
Versions.VerifyMode = VerifyMode.Size;
var initialize = Versions.InitializeAsync();
await initialize.Task;
if (initialize.status != OperationStatus.Success)
{
// TODO: 提示失败
return;
}
}
}
}
- 加载场景文件
var asset = Scene.LoadAsync("Assets/AB/B/SampleScene.unity");
await asset.Task;
- 加载资源文件
var asset= Asset.LoadAsync("Assets/AB/A/Room03_carpet_9_0.png", typeof(Sprite));
await asset.Task;
GetComponent<SpriteRenderer>().sprite = asset.asset as Sprite;
2. 网络热更示例
public class GameLauncher : MonoBehaviour
{
private enum Stage
{
Initializing,
CheckingUpdate,
Updating,
CheckingDownload,
Downloading,
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void InitializeBeforeSceneLoad()
{
Application.backgroundLoadingPriority = ThreadPriority.High;
Application.targetFrameRate = 60;
}
private const string LuaModule = "lua_script";
public Image progressImage;
public TextMeshProUGUI progressText;
private Stage _stage = Stage.Initializing;
private float _time;
private WeakReference<Tweener> _progressAction;
[RuntimeInitializeOnLoadMethod]
private static void InitializeLocalization()
{
LanguageMgr.Instance.CustomLoader = language =>
{
var path = $"Assets/Localization/lang/{language}.json";
var asset = Asset.Load(path, typeof(TextAsset));
if (asset == null)
{
return "{}";
}
var ret = Encoding.UTF8.GetString((asset.asset as TextAsset).bytes);
asset.Release();
return ret;
};
}
// 不能用Awake,xasset.PathManager在第一个场景加载后初始化
private void Start()
{
Initialize();
}
private void ProgressTo(float time, float percent)
{
if (_progressAction != null && _progressAction.TryGetTarget(out var tweener))
{
tweener.Kill();
}
tweener = progressImage.rectTransform.DOSizeDelta(new Vector2((436 - 34) * percent + 34, 42), time);
_progressAction = new WeakReference<Tweener>(tweener);
}
private async void Initialize()
{
_stage = Stage.Initializing;
_time = 0;
// xasset初始化,如解包等
if (!Versions.Initialized)
{
Versions.VerifyMode = VerifyMode.Size;
var initialize = Versions.InitializeAsync();
await initialize.Task;
if (initialize.status != OperationStatus.Success)
{
// TODO: 提示失败
InitializeFailed();
return;
}
LanguageMgr.Instance.UpdateLanguageDict();
Downloader.DownloadURL = "https://downloadpath.com/Bundles/";
}
ProgressTo(0.01f, 0.5f);
_stage = Stage.CheckingUpdate;
_time = 0;
var checkUpdateAsync = Versions.CheckUpdateAsync();
await checkUpdateAsync.Task;
if (checkUpdateAsync.status != OperationStatus.Success)
{
// TODO: 提示失败
CheckUpdateFailed();
return;
}
_stage = Stage.Updating;
_time = 0;
if (checkUpdateAsync.downloadSize > 0)
{
var downloadAsync = checkUpdateAsync.DownloadAsync();
// TODO: 更新
downloadAsync.updated += files => { };
await downloadAsync.Task;
if (downloadAsync.status != OperationStatus.Success)
{
// TODO: 提示失败
UpdateFailed();
return;
}
}
ProgressTo(0.1f, 0.5f);
_stage = Stage.CheckingDownload;
_time = 0;
var downloadSizeAsync = Versions.GetDownloadSizeAsync("lua_script", "loading");
await downloadSizeAsync.Task;
if (downloadSizeAsync.status != OperationStatus.Success)
{
// TODO: 提示失败
CheckDownloadFailed();
return;
}
_stage = Stage.Downloading;
_time = 0;
if (downloadSizeAsync.downloadSize > 0)
{
var downloadAsync = downloadSizeAsync.DownloadAsync();
// TODO: 更新
downloadAsync.updated += files => { ProgressTo(0.01f, files.downloadedBytes - files.totalSize); };
await downloadAsync.Task;
if (downloadAsync.status != OperationStatus.Success)
{
// TODO: 提示失败
DownloadFailed();
return;
}
}
else
{
ProgressTo(0.5f, 1);
}
LanguageMgr.Instance.UpdateLanguageDict();
print("脚本下载成功, 启动游戏");
LuaManager.Instance.StartGame();
}
private void InitializeFailed()
{
print("初始化失败");
}
private void CheckUpdateFailed()
{
print("检查更新失败");
}
private void UpdateFailed()
{
print("更新失败");
}
private void CheckDownloadFailed()
{
print("检查下载失败");
}
private void DownloadFailed()
{
print("下载失败");
}
private void Update()
{
_time += Time.deltaTime;
}
}
到了这里,关于Unity--AB包管理插件-xasset的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!