示例#1
0
    public IEnumerator CoGetSpriteFromAtlasAsync(string name, LoadSpriteDelegate onLoadComplete)
    {
        FirebaseManager.CheckWaitForReady((object sender, bool success) =>
        {
            if (success)
            {
                FirebaseManager.LogCrashlytics($"{Const.CRASHLYTIC_LOG_PICTURE}_{name}");
            }
        });
        //check if atlas exist
        string atlasAddress = GetAtlasAddressFromPictureName(name);

        ResourceRequest request = null;
        SpriteAtlas     atlas   = null;

        if (coroutineIsRunning)
        {
            Debug.LogWarning("This asynchronous operation is already being yielded from another coroutine.");
        }
        while (coroutineIsRunning)
        {
            yield return(0);
        }

        coroutineIsRunning = true;
        yield return(StartCoroutine(ResourcesPreloader.CoLoad <SpriteAtlas>(atlasAddress, (sender, cachedOperation) =>
        {
            atlas = cachedOperation as SpriteAtlas;
        })));

        coroutineIsRunning = false;

        //ResourceRequest request = Resources.LoadAsync<SpriteAtlas>(atlasAddress);
        //yield return request;

        if (atlas == null)
        {
            atlasAddress = GetAtlasAddressFromPictureName("OTHER");

            //request = Resources.LoadAsync<SpriteAtlas>(atlasAddress);
            //yield return request;

            yield return(StartCoroutine(ResourcesPreloader.CoLoad <SpriteAtlas>(atlasAddress, (sender, cachedOperation) =>
            {
                atlas = cachedOperation as SpriteAtlas;
            })));
        }
        var    req = request;
        Sprite spr = atlas.GetSprite(name);

        if (spr == null)
        {
            Debug.LogError($"Sprite [{name}] not found in atlas {atlasAddress}");
        }
        onLoadComplete?.Invoke(spr);
    }
    public static async Task <Sprite> GetSpriteFromAtlasAsync(string name)
    {
        FirebaseManager.CheckWaitForReady((object sender, bool success) =>
        {
            if (success)
            {
                FirebaseManager.LogCrashlytics($"{Const.CRASHLYTIC_LOG_PICTURE}_{name}");
            }
        });
        //check if atlas exist
        string atlasAddress = GetAtlasAddressFromPictureName(name);
        AsyncOperationHandle <IList <IResourceLocation> > handle = Addressables.LoadResourceLocationsAsync(atlasAddress);
        await handle.Task;
        bool atlasExist = handle.Result.Count > 0;

        //.Log($"Atlas Exist: {atlasExist}");
        Addressables.Release(handle);

        //if not, use OTHER atlas
        if (!atlasExist)
        {
            atlasAddress = GetAtlasAddressFromPictureName("OTHER");
        }

        //return sprite from atlas
        var cachedAddressable = await AddressablePreloader.Load <SpriteAtlas>(atlasAddress);

        SpriteAtlas atlas = cachedAddressable.Result as SpriteAtlas;

        if (atlas != null)
        {
            Sprite spr = atlas.GetSprite(name);
            if (spr == null)
            {
                Debug.LogError($"Sprite [{name}] not found in atlas {atlasAddress}");
            }
            return(spr);
        }
        else
        {
            Debug.LogError($"Atlas addressable [{name}] not found {atlasAddress}");
            return(null);
        }

        /*var asyncOperationHandle = Addressables.LoadAssetAsync<SpriteAtlas>(atlasAddress);
         * await asyncOperationHandle.Task;
         * if (asyncOperationHandle.Result != null)
         *  return asyncOperationHandle.Result.GetSprite(name);
         * else return null;*/
    }