当前位置:网站首页>Unity notes - use of addressables
Unity notes - use of addressables
2022-07-23 07:25:00 【گ This is a QQ name】
Preface
Addressables yes Unity A new tool officially issued to replace the old version :
The first is function , Compare with the old version AB Package tools , There is no longer only the basic function of packaging . Except packaging , And memory analysis , Local load AB resources , The resource server loads resources , It also provides a download tool that can be downloaded directly to the local , Resource encryption , Generate update packages and other functions ...
Use
The first is how to use :
First, let's talk about how to package : After downloading the plug-in ,windows Interface opening tool

top left corner Creat You can directly create a new group , My personal packaging process is as follows :
Regardless of resource references , My usual development habits are naturally one kind of resources put together , such as :

such as prefab Put a folder ,sprites Put a folder , So we can also directly drag the folder to adrressable panel , However, in this case, if it is packaged in a group , It will become a whole AB package , Of course we don't want to , So individuals use label grouping and packaging .

Just change here .
Set packaging Directory : The server loading method is directly adopted here , Set a package directory and a resource server qualification by yourself :




Check your Bilid Remote Catalog
content state buidl path Update the reference file for your increment , The directory can be set by yourself or not in Assets Under the addressable Found in the directory of
Build&load Path Don't forget to change it into your own
The rest doesn't matter , Set on demand
among Disable cataloge Updae On If not checked, the directory will be automatically updated , I don't want it to be like this, so check , The following code loading method

This is the packing button : Don't forget to modify your grouping path before packaging

Just to summarize : Is to set the grouping , Drag in the resources you want to package , A group is a AB package ( Without modifying the packaging method ), Set up the resources that need to be packaged , Set the path of each group , In fact, it can be packed at this time , If you want a hot update , Just put Bilid Remote Catalog Check , If you want to update incrementally , Just click... After packaging once build Below update a build The button , The pop-up interface allows you to choose .bin file , On your own Assets/adrressable/windows Under the table of contents , If you don't want to update directly, check Disable cataloge Updae On, After packaging, put the file into your own resource server , The rest of the tools will help you deal with it
Update the catalog + Resource code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.UI;
using System;
public class UpdateCatalogAndAssets : MonoBehaviour
{
List<object> cusKeys = new List<object>();
private void Start()
{
checkUpdate(() => { print(" Resource loading is complete , The rest can be used normally ")});
// Load several resources API
//
AsyncOperationHandle res = Addressables.InstantiateAsync("BGSprite/Panel.prefab");
AsyncOperationHandle res2 = Addressables.InstantiateAsync("Assets/Resources_moved/AA/Cube.prefab");
Addressables.LoadAssetAsync<Sprite>("BGSprite/bg_login_clan.png");
Addressables.Release(res );
}
void checkUpdate(System.Action pFinish)
{
// Debug.LogError(" checkUpdate >>>");
StartCoroutine(Initialize(() =>
{
StartCoroutine(checkUpdateSize((oSize, oList) =>
{
if (oList.Count > 0)
{
StartCoroutine(DoUpdate(oList, () =>
{
pFinish();
}));
}
else
{
pFinish();
}
}));
}));
}
/// <summary>
/// initialization
/// </summary>
/// <param name="pOnFinish"></param>
/// <returns></returns>
IEnumerator Initialize(System.Action pOnFinish)
{
// Debug.LogError(" Initialize >>>");
// initialization Addressable
var init = Addressables.InitializeAsync();
yield return init;
//Caching.ClearCache();
// Addressables.ClearResourceLocators();
//Addressables.InternalIdTransformFunc = InternalIdTransformFunc;
pOnFinish.Invoke();
}
/// <summary>
/// Check the update file size
/// </summary>
/// <param name="pOnFinish"></param>
/// <returns></returns>
IEnumerator checkUpdateSize(System.Action<long, List<string>> pOnFinish)
{
//Debug.LogError(" checkUpdateSize >>>");
long sizeLong = 0;
List<string> catalogs = new List<string>();
AsyncOperationHandle<List<string>> checkHandle = Addressables.CheckForCatalogUpdates(false);
yield return checkHandle;
if (checkHandle.Status == AsyncOperationStatus.Succeeded)
{
catalogs = checkHandle.Result;
}
/*IEnumerable<IResourceLocator> locators = Addressables.ResourceLocators;
List<object> keys = new List<object>();
// Violence traverses all key
foreach (var locator in locators)
{
foreach (var key in locator.Keys)
{
keys.Add(key);
}
}*/
//Debug.Log("download start catalogs keys is :" + keys.Count);
Debug.Log("download start catalogs count is :" + catalogs.Count);
pOnFinish.Invoke(sizeLong, catalogs);
}
/// <summary>
/// Download update logic
/// </summary>
/// <param name="catalogs"></param>
/// <param name="pOnFinish"></param>
/// <returns></returns>
IEnumerator DoUpdate(List<string> catalogs, System.Action pOnFinish)
{
//Debug.LogError(" DocatalogUpdate >>>");
var updateHandle = Addressables.UpdateCatalogs(catalogs, false);
yield return updateHandle;
foreach (var item in updateHandle.Result)
{
cusKeys.AddRange(item.Keys);
}
Addressables.Release(updateHandle);
StartCoroutine(DownAssetImpl(pOnFinish));
}
public IEnumerator DownAssetImpl(Action pOnFinish)
{
var downloadsize = Addressables.GetDownloadSizeAsync(cusKeys);
yield return downloadsize;
Debug.Log("start download size :" + downloadsize.Result);
if (downloadsize.Result > 0)
{
var download = Addressables.DownloadDependenciesAsync(cusKeys, Addressables.MergeMode.Union);
yield return download;
//await download.Task;
Debug.Log("download result type " + download.Result.GetType());
foreach (var item in download.Result as List<UnityEngine.ResourceManagement.ResourceProviders.IAssetBundleResource>)
{
var ab = item.GetAssetBundle();
Debug.Log("ab name " + ab.name);
foreach (var name in ab.GetAllAssetNames())
{
Debug.Log("asset name " + name);
}
}
Addressables.Release(download);
}
Addressables.Release(downloadsize);
pOnFinish?.Invoke();
}
}
Same as the code above
using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.UI;
public class MyLoad : MonoBehaviour
{
List<object> cusKeys = new List<object>();
public Text text;
public Button btn;
void Start()
{
btn.onClick.AddListener(() =>
{
StartCoroutine(StartDownload());
});
}
IEnumerator StartDownload()
{
text.text = "StartDown";
yield return Addressables.InitializeAsync();
List<string> catalogs = new List<string>();
AsyncOperationHandle<List<string>> checkHandle = Addressables.CheckForCatalogUpdates(false);
yield return checkHandle;
if (checkHandle.Status == AsyncOperationStatus.Succeeded)
{
catalogs = checkHandle.Result;
}
if (catalogs.Count == 0)
{
Debug.Log(" There are no resources to update , Directly enter the game ");
}
else
{
Debug.Log(" Resource directory update detected :" + catalogs.Count);
var updateHandle = Addressables.UpdateCatalogs(catalogs, false);
yield return updateHandle;
foreach (var item in updateHandle.Result)
{
cusKeys.AddRange(item.Keys);
}
var downloadsize = Addressables.GetDownloadSizeAsync(cusKeys);
yield return downloadsize;
if (downloadsize.Result > 0)
{
Debug.Log(" Detected resource size that needs to be updated :" + downloadsize.Result);
var download = Addressables.DownloadDependenciesAsync(cusKeys, Addressables.MergeMode.Union);
while (!download.IsDone)
{
text.text = download.PercentComplete.ToString();
yield return null;
}
yield return download;
foreach (var item in download.Result as List<UnityEngine.ResourceManagement.ResourceProviders.IAssetBundleResource>)
{
var ab = item.GetAssetBundle();
Debug.Log("ab name " + ab.name);
foreach (var name in ab.GetAllAssetNames())
{
Debug.Log("asset name " + name);
}
}
Addressables.Release(download);
}
else
{
Debug.Log(" There are no resources to update , Directly enter the game ");
}
Addressables.Release(downloadsize);
Addressables.Release(updateHandle);
}
Addressables.Release(checkHandle);
AsyncOperationHandle res2 = Addressables.InstantiateAsync("Assets/Resources_moved/AA/Cube.prefab");
}
}
边栏推荐
- 常见的跨域问题
- 【计网实验报告】Cisco局域网模拟组建、简单网络测试
- LAN SDN technology hard core insider - 02 forward multi-core technology for Moore's law for one second
- 局域网SDN技术硬核内幕 - 02 前传 多核技术为摩尔定律续一秒
- CAN总线详解
- 【JDBC】报错Exception in thread “main”com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communica
- Computer prompts how to deal with insufficient memory. The solution of insufficient computer C disk
- Q6ui布局操作
- Redis basic type common commands
- Detailed explanation of CAN bus
猜你喜欢

rs485通信OSI模型网络层

对比学习下的跨模态语义对齐是最优的吗?---自适应稀疏化注意力对齐机制 IEEE Trans. MultiMedia

小黑啃leetcode:589. N 叉树的前序遍历

深度学习模型的版权保护研究综述

0day attack path prediction method based on network defense knowledge map

什么是真正的 HTAP ?(二)挑战篇

【随笔】再见IE浏览器,一个时代的印记从此消失

OWA动态密码短信认证方案,解决outlook邮件双因子认证问题

数据库系统设计:分区

Overview of the development of pseudo defense in cyberspace: from pseudo concept to "pseudo +" ecology
随机推荐
Overview of the development of pseudo defense in cyberspace: from pseudo concept to "pseudo +" ecology
谷歌云和甲骨文云“热崩了”?部署跨云容灾势在必行!
Shell script
SDN application layer DDoS attack defense mechanism based on API call management
How to solve the problem that telnet is not an internal or external command? Telnet is not an internal or external command solution
LAN SDN technology hard core insider - what's in the prequel CPU?
String in SQL Server_ Implementation of split function
正向代理,反向代理及XFF
一文理解分布式开发中的服务治理
局域网SDN技术硬核内幕 13 二 从局域网到互联网
【随笔】再见IE浏览器,一个时代的印记从此消失
postman “status“: 500, “error“: “Internal Server Error“, “message“: “The request was rejecte“
ESP32:Arduino教程汇总
Fileinputformat.setinputpaths multipath read rule
rs485通信OSI模型网络层
Redis basic type common commands
Countermeasure and defense method based on softmax activation transformation
What if the file copied by SSD is only dozens of KB? Solution to slow copying speed after installing hard disk in computer
J9数字论:什么是 Web3.0?Web3.0 有哪些特征?
银行虚拟人技术应用领域及作用介绍