当前位置:   article > 正文

Unity Addressables加载资源方式汇总_unity loadassetasync

unity loadassetasync

案例工程

加载方式

异步加载的方式:
[加载分配的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;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这里插入图片描述

使用地址直接加载
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;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述
加载图集
在这里插入图片描述

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; 
        } 
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

加载场景
在这里插入图片描述

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/102645
推荐阅读
相关标签
  

闽ICP备14008679号