Addressables简单使用指南AddressablesManual

更新时间:2023-06-19 05:21:22 阅读: 评论:0

Addressables简单使⽤指南AddressablesManual 异步加载
资源地址就是在Addressables Groups⾥看到的具体资源的地址,只要给定地址就能找到需要的资源
使⽤地址加载
// 参数为资源地址,包含后缀.prefab,handle.Result就是实例化的物体,⽽不是prefab
Addressables.InstantiateAsync("address of prefab");
// 参数为资源地址,包含资源后缀名
Addressables.LoadAstAsync("address of ast");
使⽤直接资源引⽤AstReference加载
AstReference ast;
// 参数为资源地址,包含后缀.prefab
Addressables.InstantiateAsync(ast);
/
/ 参数为资源地址,包含资源后缀名
Addressables.LoadAstAsync(ast);
异步回调
匿名委托
Addressables.InstantiateAsync($"address of prefab").Completed +=(handle)=>
{
if(handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
var obj = handle.Result;
}
}
回调函数
Addressables.InstantiateAsync($"address of prefab").Completed += OnCompleted;
private void OnCompleted(AsyncOperationHandle<SceneInstance> handle)
{
if(handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
var obj = handle.Result;
}
}
async
public static async void PlaySoundEffect(AstReference audioClipAst)
{
var handle = audioClipAst.LoadAstAsync<AudioClip>();
await handle.Task;
if(handle.Status == AsyncOperationStatus.Succeeded)where did you go
{
var audioClip = handle.Result;
}
}
async Task
private async static Task<T>LoadAstAsync<T>()where T : Object
{
var path ="address of prefab";
var handle = Addressables.LoadAstAsync<T>(path);
await handle.Task;
if(handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
return handle.Result;
}
return null;
}
public static async Task<T>InstantiateAstAsync<T>()where T : Object
{
var obj =await LoadAstAsync<T>();
}
协程
public IEnumerator Start(){
AsyncOperationHandle<Texture2D> handle = Addressables.LoadAstAsync<Texture2D>("mytexture");
cry on my shoulder歌词yield return handle;
if(handle.Status == AsyncOperationStatus.Succeeded){
Texture2D texture = handle.Result;
// The texture is ready for u.
// ...
// Relea the ast after its u:
Addressables.Relea(handle);
}
}
同步加载
if can not find, go to Unity github repository and find the repository named addresables-sample
本地打包流程
在AddressableAstsData/AstGroups下找到对应Group的配置,这⾥可以查看到Build Path的本地路径。
根据需要修改Advanced Options -> Bundle Mode
Pack Together:每个Group中,除了场景之外的资源,打成⼀个包,场景另外打⼀个包
Pack Separately:每个Group中的Entry分别打⼀个包,Entry即Group下的各个⽬录
Pack Together By Label:相同Label打⼀个包
⼿动打包:Addressables Group界⾯下,Build -> New Build -> Default Build Script
主菜单File -> Build Settings -> Build
AstReference filter 过滤器
如果在Addressables Group界⾯⾥以⽂件夹为单位创建Entry,那么Inspector下的AstReference打开的过滤器中会看到很多可选的筛选指定类型的ast
[Serializable]
public class AstReferenceSceneAst : AstReferenceT<SceneAst>
{
public AstReferenceSceneAst(string guid):ba(guid){}
}
SceneAst可换成其他资源类型,具体可通过在Project窗⼝右键Create,弹出的类型都是资源类型
筛选Component
ComponentReference.cs
using UnityEngine;
using UnityEngine.AddressableAsts;
using UnityEngine.ResourceManagement.AsyncOperations;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class ComponentReference<TComponent>: AstReference where TComponent : Component
{
public ComponentReference(string guid):ba(guid)
{
}
public new AsyncOperationHandle<TComponent>InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent =null)
{
return Addressables.ResourceManager.CreateChainOperation<TComponent, GameObject>(ba.InstantiateAsync(position, Quaternion.identity, pare nt), GameObjectReady);
}
纽约时报畅销书
public new AsyncOperationHandle<TComponent>InstantiateAsync(Transform parent =null,bool instantiateInWorldSpace =fal)
{
return Addressables.ResourceManager.CreateChainOperation<TComponent, GameObject>(ba.InstantiateAsync(parent, instantiateInWorldSpace), GameObjectReady);
}
public AsyncOperationHandle<TComponent>LoadAstAsync()
{
return Addressables.ResourceManager.CreateChainOperation<TComponent, GameObject>(ba.LoadAstAsync<GameObject>(), GameObjectRe ady);
}
AsyncOperationHandle<TComponent>GameObjectReady(AsyncOperationHandle<GameObject> arg)
{
var comp = arg.Result.GetComponent<TComponent>();
return Addressables.ResourceManager.CreateCompletedOperation<TComponent>(comp,string.Empty);
}
public override bool ValidateAst(Object obj)
{
var go = obj as GameObject;
return go !=null&& go.GetComponent<TComponent>()!=null;
}
public override bool ValidateAst(string path)
{
#if UNITY_EDITOR
//this load can
var go = AstDataba.LoadAstAtPath<GameObject>(path);
return go !=null&& go.GetComponent<TComponent>()!=null;
#el
return fal;
#endif
}fmm
public void ReleaInstance(AsyncOperationHandle<TComponent> op)
{
// Relea the instance
var component = op.Result as Component;
if(component !=null)
{
Addressables.ReleaInstance(component.gameObject);
}
// Relea the handle
// Relea the handle
Addressables.Relea(op);
}
}
boa constrictor[Serializable]
public class ComponentReferenceYourComponent : ComponentReference<YourComponent>
初二上册英语
{报价技巧
public ComponentReferenceYourComponent(string guid)
:ba(guid){}
}
YourComponent为你的继承⾃Component的脚本组件
BUG
找不到Sprite资源
Addressables.LoadAstAsync("folder/sprite.png");
假设你的sprite在Addressables中的地址为folder/sprite.png
当使⽤版本⼤于1.3.8的Addressables时,在使⽤⾮U Exsisting Build的模式时,⽆法找到资源。
但当你的sprite的地址为sprite.png,不带任何⽗⽬录时,则不会出现以上问题。
正确的使⽤⽅法
使⽤版本1.3.8(或者更低版本,⽬前1.3.8 < 版本 <= 1.7.4的均存在该BUG)
Addressables.LoadAstAsync("folder/sprite.png[sprite]");
about baidu
/
/ 对于sprite atlas中只有⼀个sprite的情形,最后的[sprite]是可以省略的,以下代码也有效
Addressables.LoadAstAsync("folder/sprite.png");
// 对于sprite atlas中有多个sprite的情形,最后的[sprite_0]不能省略,[sprite_0]为sprite atlas中的⽬标sprite名称
Addressables.LoadAstAsync("folder/sprite.png[sprite_0]");
初始场景载⼊之前就使⽤异步加载,⼀直加载不完成
对接英文
⽐如使⽤了标签[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]的游戏启动函数中,在游戏加载初始场景之前使⽤Addressables进⾏异步加载,则⼀直不会加载完成
解决⽅法就是在初始空场景中⽤⼀个Mono脚本的Start来作为游戏启动函数
让⽂件夹Addressable,⽂件夹名称和内部的资源名称有重复的部分,引发InvalidKeyException
string GetInternalIdFromFolderEntry(string keyStr, AddressableAstEntry entry)
{
var entryPath = entry.AstPath;
if(keyStr.StartsWith(entry.address +"/"))
return keyStr.Replace(entry.address, entryPath);
foreach(var l in entry.labels)
if(keyStr.StartsWith(l +"/"))
return keyStr.Replace(l, entryPath);
return string.Empty;
}
keyStr ="Config/DebugConfigData.ast";
entry.address ="Config";
entryPath ="Asts/AddressablesResources/Config";
Debug.Log(keyStr.Replace(entry.address, entryPath)=="Asts/AddressablesResources/Config/DebugAsts/AddressablesResources/ConfigData.ast") ;
⽂件夹名称为aaa,路径为"Asts/aaa"
资源名称为aaabbb.ast
medical是什么意思勾选aaa⽂件夹的Addressable选项,那么资源的地址就是"aaa/aaabbb.ast"
仅当有⽂件夹时才会进⼊到这个函数,⽽Replace会将aaa替换为全路径,即"aaa" -> “Asts/aaa”,所以最终结果
是"Asts/aaa/Asts/aaabbb.ast"。绝了!你特么不会⽤IndexOf和SubString来分割字符串吗?
重命名资产的Address后,报错InvalidKeyException
Addressables 1.8.4版本
Project Settings -> Editor -> Enter Play Mode Options 勾选
Window -> Ast Management -> Addressables -> Groups -> Play Mode Script 选择 U Ast Databa
修改某个资源为错误的Address,在加载时会报错InvalidKeyException
将资源的Address修正,再次加载,仍会报错InvalidKeyException
将Project Settings -> Editor -> Enter Play Mode Options 不勾选(或者勾选Reload Domain),则加载正常
不执⾏上⼀步,⽽是改为重启Unity编辑器,也会加载正常
猜测是游戏启动后会先执⾏重建资源对应key的动作,再开始游戏。可能是某个静态构造函数内执⾏的,因为 Enter Play Mode Options 勾选 会导致静态类创建后即使下⼀次启动游戏也不会被销毁(静态类⽣命周期和Unity编辑器⼀致),静态构造函数就不会再次执⾏,但也可能是其他机制实现的
Addressables 1.16.10或更⾼版本
Project Settings -> Editor -> Enter Play Mode Options 勾选
Window -> Ast Management -> Addressables -> Groups -> Play Mode Script 选择 U Ast Databa
修改某个资源为错误的Address,在加载时会报错InvalidKeyException
将资源的Address修正,再次加载,仍会报错InvalidKeyException
将Project Settings -> Editor -> Enter Play Mode Options 不勾选(或者勾选Reload Domain),加载仍报错
懒得从源码找原因了,如果遇到这个问题,注意把Addressables版本回退到1.8.4或者其他有效版本
参考

本文发布于:2023-06-19 05:21:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/150203.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:资源   地址   游戏   加载   启动   不会   类型   勾选
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图