seefood_diet / Assets / UIKit / AssetHelper.cs
AssetHelper.cs
Raw
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public static class AssetHelper {

    public static readonly string ROOT_PATH = "Assets/AddressableResources/";

    public static async Task<T> LoadAssetAsync<T> (string key) where T : UnityEngine.Object {

        if (Application.isPlaying) {

            var handle = Addressables.LoadAssetAsync<T> (key);
            await handle.Task; // wait for the task to complete before we try to get the result

            if (handle.Status != AsyncOperationStatus.Succeeded) {
                Debug.Log ("LoadAssetAsync status: " + handle.Status.ToString ());
            }

            T result = (T) handle.Result;
            // Addressables.Release(handle); // DO NOT release the handle! that would make the loaded asset null when in virtual mode or packed mode

            return result;

        } else {

#if UNITY_EDITOR
            return UnityEditor.AssetDatabase.LoadAssetAtPath<T> (ROOT_PATH + key);
#else
            return null;
#endif
        }
    }
}