Unity 笔记——Addressables的使用

前言

Addressables是Unity官方出的代替旧版的一个新工具:

首先是功能,对比旧版的AB包工具,不再只有打包这一基本功能了。除打包之外,还有内存分析,本地加载AB资源,资源服务器加载资源,并且提供了下载工具可以直接下载到本地,资源加密,生成更新包等等功能。。。

使用

首先是使用方法:

先说第一点如何打包:下载插件之后,windows界面打开工具 

 左上角Creat直接可以新建分组,我个人的打包流程如下:

在不考虑资源引用的情况下,我平时的开发习惯自然是一个种类的资源放在一起了,比如:

 比如prefab放一个文件夹,sprites放一个文件夹,所以我们也可以直接将文件夹拖入到adrressable面板,不过这样的话打包出在一个组的话,就会变成一整个AB包,我们当然不想这样,所以个人采用标签分组打包的方式。

改这里就可以了。

设置打包目录:这里直接采用服务器加载的方式,自己定一个出包目录和一个资源服务器资质即可:

 

 勾选上你的Bilid Remote Catalog

content state buidl path 为你的增量更新对比文件,目录可以自己设置也可以不设置在Assets下的addressable的目录中找

Build&load Path别忘记改成自己的

剩下的无所谓,按需设置

其中Disable cataloge Updae On如果不勾选的话会自动更新目录,个人不想要它这样所以勾选上,后面上代码加载方法

这个是打包按钮 :打包之前别忘记把自己的分组路径也修改了

 简单总结一下:就是设置分组,拖入自己想要打包的资源,一个分组就是一个AB包(在你不修改打包方式的情况下),设置好需要打包的资源,设置你每个分组的路径,其实这个时候就已经可以打包了,如果你想热更新的话,就把Bilid Remote Catalog勾选,如果你想增量更新的话,就在打包一次之后点击build下面的update a build的按钮,弹出界面让你选择。bin文件,在你自己Assets/adrressable/windows目录下,如果不想上来就直接更新就勾选Disable cataloge Updae On,打包之后文件放入自己的资源服务器,剩下的事情工具就全会帮你处理了

更新目录+资源代码

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("资源加载完事了,剩下的正常使用即可")});
        //加载资源的几个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>
	/// 初始化
	/// </summary>
	/// <param name="pOnFinish"></param>
	/// <returns></returns>
	IEnumerator Initialize(System.Action pOnFinish)
	{
		//		Debug.LogError(" Initialize >>>");
		//初始化Addressable
		var init = Addressables.InitializeAsync();
		yield return init;
		//Caching.ClearCache();
		// Addressables.ClearResourceLocators();
		//Addressables.InternalIdTransformFunc = InternalIdTransformFunc;
		pOnFinish.Invoke();
	}
	/// <summary>
	/// 检查更新文件大小
	/// </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>();
		//暴力遍历所有的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>
	/// 下载更新逻辑
	/// </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();
	}
}

和上面代码一样

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("没有需要更新的资源,直接进入游戏即可");
		}
		else
		{
			Debug.Log("检测到资源目录更新:" + 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("检测到需要更新的资源大小:" + 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("没有需要更新的资源,直接进入游戏即可");
			}
			Addressables.Release(downloadsize);
			Addressables.Release(updateHandle);
		}

		Addressables.Release(checkHandle);
		AsyncOperationHandle res2 = Addressables.InstantiateAsync("Assets/Resources_moved/AA/Cube.prefab");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_55042292/article/details/125931439
今日推荐