天天看點

Unity Addressables加載資源方式彙總

案例工程

加載方式

異步加載的方式:

[加載配置設定的Sprite或Atlas的方法].Completed+ = [加載Sprite後要執行的代碼];

[加載指定的Sprite或Atlas的方法]随Sprite類型和配置設定方法而變化。

[Sprite加載後将執行的代碼]可以是委托,也可以是lambda表達式(在較簡單的情況下)。

用加載sprite為例

使用可尋址對象加載

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;
        }
    }
}
           
Unity Addressables加載資源方式彙總

使用位址直接加載

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加載資源方式彙總

加載圖集

Unity Addressables加載資源方式彙總

Unity官方:

如果同時将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<SpriteRenderer>();
        //if (useAtlasedSpriteName)
        //{
        //    string atlasedSpriteAddress = spriteAtlasAddress + '[' + atlasedSpriteName + ']';
        //    Addressables.LoadAssetAsync<Sprite>(atlasedSpriteAddress).Completed += SpriteLoaded;
        //}
        //else
        //    Debug.Log(newAtlasedSprite.editorAsset.name);
        //    newAtlasedSprite.LoadAssetAsync<Sprite>().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加載資源方式彙總
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;
    }
}
           

繼續閱讀