1.安装Addressable PackManager
2.把你想要打成AB的任何资源,单击,在检视面板勾上Addressable,命名
3.如果挂载式加载,在脚本声明字段AssetReference或者其子类,拖拽赋值即可
4.加载Sprite
public List<SpriteRenderer> spritesToChange;
方法一
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;public AssetReferenceSprite singleSpriteReference;
void GetSprite()
{
singleSpriteReference.LoadAssetAsync<Sprite>().Completed += SingleDone;
}
void SingleDone(AsyncOperationHandle<Sprite> op)
{
if (op.Result == null)
{
Debug.LogError("no sprites here.");
return;
}
spritesToChange[0].sprite = op.Result;
}
方法二
Addressables.LoadAssetAsync<Sprite>("Atlas[u7]")
4.1加载图集
方法一
public AssetReferenceAtlas spriteAtlasReference;
spriteAtlasReference.LoadAssetAsync().Completed += AtlasDone;
void AtlasDone(AsyncOperationHandle<SpriteAtlas> op)
{
if (op.Result == null)
{
Debug.LogError("no atlases here.");
return;
}spritesToChange[2].sprite = op.Result.GetSprite("u3");
}
方法二
public AssetReference spriteSheetReference;
spriteSheetReference.LoadAssetAsync<IList<Sprite>>().Completed += SheetDone;
void SheetDone(AsyncOperationHandle<IList<Sprite>> op)
{
if (op.Result == null)
{
Debug.LogError("no sheets here.");
return;
}spritesToChange[1].sprite = op.Result[5];
}
5.加载场景
Addressables.LoadSceneAsync("key");