Unity Addressables加载资源方式汇总

案例工程

加载方式

异步加载的方式:
[加载分配的Sprite或Atlas的方法].Completed+ = [加载Sprite后要执行的代码];
[加载指定的Sprite或Atlas的方法]随Sprite类型和分配方法而变化。
[Sprite加载后将执行的代码]可以是委托,也可以是lambda表达式(在较简单的情况下)。

用加载sprite为例

使用可寻址对象加载


```csharp
using UnityEngine;
using System.Collections;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
public class SpriteLoader : MonoBehaviour
{
    public AssetReferenceSprite newSprite;
    private SpriteRenderer spriteRenderer;
    // Use this for initialization
    void Start()
    {
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        newSprite.LoadAssetAsync().Completed += SpriteLoaded;
    }
    private void SpriteLoaded(AsyncOperationHandle<Sprite> obj)
    {
        switch (obj.Status)
        {
            case AsyncOperationStatus.Succeeded:
                spriteRenderer.sprite = obj.Result;
                break;
            case AsyncOperationStatus.Failed:
                Debug.LogError("Sprite load failed.");
                break;
            default:
                //case AsyncOperationStatus.None: 
                break;
        }
    }
}

在这里插入图片描述

使用地址直接加载

using UnityEngine;
using System.Collections;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
public class SpriteLoadFromPath : MonoBehaviour
{
    public string newSpriteAddress;
    public bool useAddress;
    public AssetReferenceSprite newSprite;
    private SpriteRenderer spriteRenderer;
    // Use this for initialization
    void Start()
    {
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        if (useAddress)
            Addressables.LoadAssetAsync<Sprite>(newSpriteAddress).Completed += SpriteLoaded;
        else
            newSprite.LoadAssetAsync().Completed += SpriteLoaded;
    }
    private void SpriteLoaded(AsyncOperationHandle<Sprite> obj)
    {
        switch (obj.Status)
        {
            case AsyncOperationStatus.Succeeded:
                spriteRenderer.sprite = obj.Result;
                break;
            case AsyncOperationStatus.Failed:
                Debug.LogError("Sprite load failed.");
                break;
            default:
                break;
        }
    }
}

Unity Addressables加载资源方式汇总_第1张图片
加载图集
Unity Addressables加载资源方式汇总_第2张图片
如果同时将Sprite Atlas和源Sprite Textures标记为可寻址,则最终的AssetBundle将包含重复的数据。有一个Addressables Analyze规则可以检测到这一点。在这种情况下,我们建议仅将Sprite Atlas标记为可寻址。

using UnityEngine;
using System.Collections;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
public class SpriteAtlasLoader : MonoBehaviour
{
    public AssetReferenceAtlasedSprite newAtlasedSprite; 
    public string spriteAtlasAddress; 
    public string atlasedSpriteName; 
    public bool useAtlasedSpriteName; 
    private SpriteRenderer spriteRenderer;
    // Use this for initialization
    void Start()
    {
        //①
        //spriteRenderer = gameObject.GetComponent();
        //if (useAtlasedSpriteName)
        //{
        //    string atlasedSpriteAddress = spriteAtlasAddress + '[' + atlasedSpriteName + ']';
        //    Addressables.LoadAssetAsync(atlasedSpriteAddress).Completed += SpriteLoaded;
        //}
        //else
        //    Debug.Log(newAtlasedSprite.editorAsset.name);
        //    newAtlasedSprite.LoadAssetAsync().Completed += SpriteLoaded;
        //②
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        Addressables.LoadAssetAsync<SpriteAtlas>(spriteAtlasAddress).Completed += SpriteAtlasLoaded;
    }
    private void SpriteLoaded(AsyncOperationHandle<Sprite> obj)
    {
        switch (obj.Status)
        {
            case AsyncOperationStatus.Succeeded:
                spriteRenderer.sprite = obj.Result;
                break;
            case AsyncOperationStatus.Failed:
                Debug.LogError("Sprite load failed.");
                break;
            default:
                break;
        }
    }
    private void SpriteAtlasLoaded(AsyncOperationHandle<SpriteAtlas> obj)
    {
        switch (obj.Status)
        {
            case AsyncOperationStatus.Succeeded:
                Debug.Log(obj.Result.spriteCount);
                spriteRenderer.sprite = obj.Result.GetSprite(atlasedSpriteName); 
                break;
            case AsyncOperationStatus.Failed: 
                Debug.LogError("Sprite load failed. Using default Sprite."); 
                break;
            default: // case AsyncOperationStatus.None: 
                break; 
        } 
    }
}

加载场景
Unity Addressables加载资源方式汇总_第3张图片

public AssetReference Scene;

 //加载场景
Scene.LoadSceneAsync().Completed += SceneLoaded;


private void SceneLoaded(AsyncOperationHandle<SceneInstance> obj)
{
    switch (obj.Status)
    {
        case AsyncOperationStatus.None:
            break;
        case AsyncOperationStatus.Succeeded:
            Scene.LoadSceneAsync(UnityEngine.SceneManagement.LoadSceneMode.Additive);
            break;
        case AsyncOperationStatus.Failed:
            break;
    }
}

你可能感兴趣的:(游戏开发,#,Unity3d,#,C#,unity3d,资源加载)