Unity 笔记——Addressables的使用

这篇具有很好参考价值的文章主要介绍了Unity 笔记——Addressables的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

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

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

使用

首先是使用方法:

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

Unity 笔记——Addressables的使用

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

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

Unity 笔记——Addressables的使用

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

Unity 笔记——Addressables的使用

改这里就可以了。

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

Unity 笔记——Addressables的使用

Unity 笔记——Addressables的使用

Unity 笔记——Addressables的使用

 Unity 笔记——Addressables的使用

 勾选上你的Bilid Remote Catalog

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

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

剩下的无所谓,按需设置

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

Unity 笔记——Addressables的使用

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

Unity 笔记——Addressables的使用

 简单总结一下:就是设置分组,拖入自己想要打包的资源,一个分组就是一个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();
	}
}

和上面代码一样文章来源地址https://www.toymoban.com/news/detail-412614.html

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");
	}
}

到了这里,关于Unity 笔记——Addressables的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity使用 Addressables 预加载所有资源,提现加载资源,发布webgl加载缓慢问题

    Addressables 我也是刚接触,知道的不是很多,基本的用法还是知道一些的 1 .在Window–Package Manager里找到Addressables进行安装   2.选择资源,点击Assets中的一个资源,在Inspector面板上就会出现一个勾选Assressable,也就是是否加入资源打包的分组,和AssetBundle分组是一个性质。选上以

    2023年04月08日
    浏览(36)
  • Unity Addressables热更流程

    一、分组(网上教程一大堆) 二、构建         构建前设置:                 1、分组设置。所有组做远端构建加载选择,RemoteBuildPath 。RemoteLoadPath                                    2、AddressableAssetSettings设置          3、构建                   三、导

    2024年02月10日
    浏览(30)
  • Unity热更新HybridCLR+Addressables

    2024年02月13日
    浏览(36)
  • Unity Addressables资源管理 主设置面板

    Addressables资源管理总目录 位置1 位置2     这个是全局路径配置的选择 可以点击 Manager Profiles 打开路径配置面板 打包路径设置   Send Profiler Events  打开这个选项,才能在Event Viewer窗口看到资源的事件 Log Runtime Exceptions  记录运行时的异常日志   默认情况下Addressables只记录警告

    2024年02月13日
    浏览(37)
  • 【Unity学习笔记】Unity TestRunner使用

    转载请注明出处:🔗https://blog.csdn.net/weixin_44013533/article/details/135733479 作者:CSDN@|Ringleader| 参考: Input testing Getting started with Unity Test Framework HowToRunUnityUnitTest 如果对Unity的newInputSystem感兴趣可以参看我这篇文章:【Unity学习笔记】第十二 · New Input System 及其系统结构 和 源码浅

    2024年01月22日
    浏览(28)
  • 使用 GPT4 和 ChatGPT 开发应用:前言到第三章

    原文:Developing Apps with GPT-4 and ChatGPT 译者:飞龙 协议:CC BY-NC-SA 4.0 在发布仅仅五天后,ChatGPT 就吸引了惊人的一百万用户,这在科技行业及其他领域引起了轰动。作为一个副作用,OpenAI API 用于人工智能文本生成的接口突然曝光,尽管它已经可用了三年。ChatGPT 界面展示了这

    2024年01月20日
    浏览(58)
  • Addressables(2) ResourceLocation和AssetReference

    var op = Addressables.LoadResourceLocationsAsync(key); var result = op.WaitForCompletion(); 把加载的Key塞进去,不难看出,IResourceLocation可以用来获得资源的详细信息 很适合用于更新分析,或者一些检查工具 https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/asset-reference-intro.html 上一篇安装的时

    2024年01月21日
    浏览(24)
  • 【Unity笔记】TimeLine的详细使用介绍

    2023年08月28日
    浏览(40)
  • Unity学习笔记--使用 C# 开发一个 LRU

    什么是 LRU 在计算机系统中,LRU(Least Recently Used,最近最少使用)是一种缓存置换算法。缓存是计算机系统中的一种能够高速获取数据的介质,而缓存置换算法则是在缓存空间不足时,需要淘汰掉部分缓存数据以腾出空间,使得后来需要访问数据能够有更多的缓存空间可用。

    2024年02月13日
    浏览(31)
  • Addressables(1) 从安装到加载单个/多个资源

    不想再配改那些狗屎路径,准备研究一下Adressable,据说可以用key加载指定的资源 刚安装下来,随便搞了个资源勾选了一下addressable的框框,多了好多东西啊 概念铺天盖地而来,ok 没事的 慢慢来! Package Manager安装 Resources加载 AssetBundle加载 Unity 2021.3.34 插件版本 1.21.17 如果跟我

    2024年01月21日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包