Beispiel #1
0
        public IEnumerator TryCacheSingleImage(string id, string uri, ESCAnimationType animatedType, Action <EnhancedImageInfo> Finally = null, int forcedHeight = -1)
        {
            if (this.CachedImageInfo.TryGetValue(id, out var info))
            {
                Finally?.Invoke(info);
                yield break;
            }
            var bytes = new byte[0];

            yield return(this.DownloadContent(uri, (b) => bytes = b));

            yield return(this.OnSingleImageCached(bytes, id, animatedType, Finally, forcedHeight));
        }
Beispiel #2
0
        public IEnumerator OnSingleImageCached(byte[] bytes, string id, ESCAnimationType animatedType, Action <EnhancedImageInfo> Finally = null, int forcedHeight = -1)
        {
            if (bytes.Length == 0)
            {
                Finally(null);
                yield break;
            }

            Sprite sprite = null;
            int    spriteWidth = 0, spriteHeight = 0;
            AnimationControllerData animControllerData = null;

            switch (animatedType)
            {
            case ESCAnimationType.GIF:
                AnimationLoader.Process(AnimationType.GIF, bytes, (tex, atlas, delays, width, height) =>
                {
                    animControllerData = AnimationController.instance.Register(id, tex, atlas, delays);
                    sprite             = animControllerData.sprite;
                    spriteWidth        = width;
                    spriteHeight       = height;
                });
                yield return(new WaitUntil(() => animControllerData != null));

                break;

            case ESCAnimationType.APNG:
                AnimationLoader.Process(AnimationType.APNG, bytes, (tex, atlas, delays, width, height) =>
                {
                    animControllerData = AnimationController.instance.Register(id, tex, atlas, delays);
                    sprite             = animControllerData.sprite;
                    spriteWidth        = width;
                    spriteHeight       = height;
                });
                yield return(new WaitUntil(() => animControllerData != null));

                break;

            case ESCAnimationType.MAYBE_GIF:
                if (6 <= bytes.Length && (this.ContainBytePattern(bytes, s_animattedGIF89aPattern) || this.ContainBytePattern(bytes, s_animattedGIF87aPattern)))
                {
                    AnimationLoader.Process(AnimationType.GIF, bytes, (tex, atlas, delays, width, height) =>
                    {
                        animControllerData = AnimationController.instance.Register(id, tex, atlas, delays);
                        sprite             = animControllerData.sprite;
                        spriteWidth        = width;
                        spriteHeight       = height;
                    });
                    yield return(new WaitUntil(() => animControllerData != null));
                }
                else
                {
                    try {
                        sprite       = GraphicUtils.LoadSpriteRaw(bytes);
                        spriteWidth  = sprite.texture.width;
                        spriteHeight = sprite.texture.height;
                    }
                    catch (Exception ex) {
                        Logger.Error(ex);
                        sprite = null;
                    }
                }
                break;

            case ESCAnimationType.WEBP:
            case ESCAnimationType.NONE:
            default:
                try {
                    sprite       = GraphicUtils.LoadSpriteRaw(bytes);
                    spriteWidth  = sprite.texture.width;
                    spriteHeight = sprite.texture.height;
                }
                catch (Exception ex) {
                    Logger.Error(ex);
                    sprite = null;
                }
                break;
            }
            var ret = this._imageInfoContaner.Spawn();

            if (sprite != null)
            {
                if (forcedHeight != -1)
                {
                    this.SetImageHeight(ref spriteWidth, ref spriteHeight, forcedHeight);
                }
                ret.ImageId            = id;
                ret.Sprite             = sprite;
                ret.Width              = spriteWidth;
                ret.Height             = spriteHeight;
                ret.AnimControllerData = animControllerData;
                this.CachedImageInfo.TryAdd(id, ret);
            }
            else
            {
                this._imageInfoContaner.Despawn(ret);
            }
            Finally?.Invoke(ret);
        }