private void CacheSpriteSheetImage(string id, ImageRect rect, Texture2D tex, Action <EnhancedImageInfo> Finally = null, int forcedHeight = -1) { int spriteWidth = rect.width, spriteHeight = rect.height; Sprite sprite = Sprite.Create(tex, new Rect(rect.x, tex.height - rect.y - spriteHeight, spriteWidth, spriteHeight), new Vector2(0, 0)); sprite.texture.wrapMode = TextureWrapMode.Clamp; EnhancedImageInfo ret = null; if (sprite != null) { if (forcedHeight != -1) { SetImageHeight(ref spriteWidth, ref spriteHeight, forcedHeight); } ret = new EnhancedImageInfo() { ImageId = id, Sprite = sprite, Width = spriteWidth, Height = spriteHeight, AnimControllerData = null }; _cachedImageInfo[id] = ret; } Finally?.Invoke(ret); }
/// <summary> /// On sprite sheet cached /// </summary> /// <param name="p_ID">ID of the sprite sheet</param> /// <param name="p_Texture">Result texture</param> /// <param name="p_Rect">Sheet rect</param> /// <param name="p_Finally">A callback that occurs after the resource is retrieved. This will always occur even if the resource is already cached.</param> /// <param name="p_ForcedHeight">Forced height</param> private static void CacheSpriteSheetImage(string p_ID, Texture2D p_Texture, ImageRect p_Rect, Action <EnhancedImageInfo> p_Finally = null, int p_ForcedHeight = -1) { int l_SpriteWidth = p_Rect.width; int l_SpriteHeight = p_Rect.height; Sprite l_Sprite = Sprite.Create(p_Texture, new Rect(p_Rect.x, p_Texture.height - p_Rect.y - l_SpriteHeight, l_SpriteWidth, l_SpriteHeight), new Vector2(0, 0)); l_Sprite.texture.wrapMode = TextureWrapMode.Clamp; EnhancedImageInfo l_Result = null; if (l_Sprite != null) { if (p_ForcedHeight != -1) { ComputeImageSizeForHeight(ref l_SpriteWidth, ref l_SpriteHeight, p_ForcedHeight); } l_Result = new EnhancedImageInfo() { ImageID = p_ID, Sprite = l_Sprite, Width = l_SpriteWidth, Height = l_SpriteHeight, AnimControllerData = null }; m_CachedImageInfo[p_ID] = l_Result; } p_Finally?.Invoke(l_Result); }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /// <summary> /// On single image cached /// </summary> /// <param name="p_ID"></param> /// <param name="p_Bytes">Result bytes</param> /// <param name="p_IsAnimated">Is and animation image</param> /// <param name="p_Finally">A callback that occurs after the resource is retrieved. This will always occur even if the resource is already cached.</param> /// <param name="p_ForcedHeight">Forced height</param> /// <returns></returns> private static IEnumerator OnSingleImageCached(string p_ID, byte[] p_Bytes, bool p_IsAnimated, Action <EnhancedImageInfo> p_Finally = null, int p_ForcedHeight = -1) { int l_SpriteWidth = 0; int l_SpriteHeight = 0; Sprite l_Sprite = null; AnimationControllerData l_AnimControllerData = null; if (p_IsAnimated) { AnimationLoader.Process(AnimationType.GIF, p_Bytes, (p_Texture, p_Atlas, p_Delays, p_Width, p_Height) => { l_AnimControllerData = AnimationController.instance.Register(p_ID, p_Texture, p_Atlas, p_Delays); l_Sprite = l_AnimControllerData.sprite; l_SpriteWidth = p_Width; l_SpriteHeight = p_Height; }); yield return(new WaitUntil(() => l_AnimControllerData != null)); } else { try { l_Sprite = SDK.Unity.Sprite.CreateFromRaw(p_Bytes); l_SpriteWidth = l_Sprite.texture.width; l_SpriteHeight = l_Sprite.texture.height; } catch (Exception p_Exception) { Logger.Instance.Error("[SDK.Chat][ImageProvider.DownloadContent] Error in OnSingleImageCached"); Logger.Instance.Error(p_Exception); l_Sprite = null; } } EnhancedImageInfo l_Result = null; if (l_Sprite != null) { if (p_ForcedHeight != -1) { ComputeImageSizeForHeight(ref l_SpriteWidth, ref l_SpriteHeight, p_ForcedHeight); } l_Result = new EnhancedImageInfo() { ImageID = p_ID, Sprite = l_Sprite, Width = l_SpriteWidth, Height = l_SpriteHeight, AnimControllerData = l_AnimControllerData }; m_CachedImageInfo[p_ID] = l_Result; } p_Finally?.Invoke(l_Result); }
private void CacheSpriteSheetImage(string id, ImageRect rect, Texture2D tex, Action <EnhancedImageInfo> Finally = null, int forcedHeight = -1) { if (tex == null) { Finally?.Invoke(null); return; } int spriteWidth = rect.Width, spriteHeight = rect.Height; var sprite = Sprite.Create(tex, new Rect(rect.X, tex.height - rect.Y - spriteHeight, spriteWidth, spriteHeight), new Vector2(0, 0)); sprite.texture.wrapMode = TextureWrapMode.Clamp; EnhancedImageInfo ret = null; if (sprite != null) { if (forcedHeight != -1) { this.SetImageHeight(ref spriteWidth, ref spriteHeight, forcedHeight); } ret = new EnhancedImageInfo() { ImageId = id, Sprite = sprite, Width = spriteWidth, Height = spriteHeight, AnimControllerData = null }; this.CachedImageInfo.TryAdd(id, ret); } Finally?.Invoke(ret); }
public IEnumerator OnSingleImageCached(byte[] bytes, string id, bool isAnimated, 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; if (isAnimated) { 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.log.Error(ex); sprite = null; } } EnhancedImageInfo ret = null; if (sprite != null) { if (forcedHeight != -1) { SetImageHeight(ref spriteWidth, ref spriteHeight, forcedHeight); } ret = new EnhancedImageInfo() { ImageId = id, Sprite = sprite, Width = spriteWidth, Height = spriteHeight, AnimControllerData = animControllerData }; _cachedImageInfo[id] = ret; } Finally?.Invoke(ret); }
private static IEnumerator <WaitUntil> WaitForCollection(IChatEmote emote, byte count) { float time = Time.time; EnhancedImageInfo enhancedImageInfo = default; yield return(new WaitUntil(() => ChatImageProvider.instance.CachedImageInfo.TryGetValue(emote.Id, out enhancedImageInfo) && mode != Mode.None)); //Log($"Continuing after {Time.time - time} seconds..."); TimeoutScript cloneTimer; PS_Prefab_Pair ps_Prefab_Pair = particleSystems[mode]; if (!ps_Prefab_Pair.Item1.ContainsKey(emote.Id)) { cloneTimer = UnityEngine.Object.Instantiate(ps_Prefab_Pair.Item2).GetComponent <TimeoutScript>(); var main = cloneTimer.PS.main; if (mode == Mode.Menu) { main.startSize = Settings.menuSize; } if (mode == Mode.Play) { main.startSize = Settings.songSize; } main.startSpeed = Settings.emoteFallspeed; main.startLifetime = (8 / (Settings.emoteFallspeed - 1)) + 1; cloneTimer.key = emote.Id; cloneTimer.mode = mode; SceneManager.MoveGameObjectToScene(cloneTimer.gameObject, myScene); ps_Prefab_Pair.Item1.Add(emote.Id, cloneTimer); //sorta working animated emotes if (emote.IsAnimated) { var tex = cloneTimer.PS.textureSheetAnimation; tex.enabled = true; tex.mode = ParticleSystemAnimationMode.Sprites; tex.timeMode = ParticleSystemAnimationTimeMode.Lifetime; int spriteCount = enhancedImageInfo.AnimControllerData.sprites.Length - 1; float timeForEmote = 0; for (int i = 0; i < spriteCount; i++) { tex.AddSprite(enhancedImageInfo.AnimControllerData.sprites[i]); timeForEmote += enhancedImageInfo.AnimControllerData.delays[i]; } //float lifeTime = cloneTimer.PS.main.startLifetime.constant * 1000; AnimationCurve curve = new AnimationCurve(); float singleFramePercentage = 1.0f / spriteCount; float currentTimePercentage = 0; float currentFramePercentage = 0; for (int frameCounter = 0; currentTimePercentage <= 1; frameCounter++) { if (frameCounter > spriteCount) { frameCounter = 0; currentFramePercentage = 0; } curve.AddKey(currentTimePercentage, currentFramePercentage); currentTimePercentage += enhancedImageInfo.AnimControllerData.delays[frameCounter] / timeForEmote; currentFramePercentage += singleFramePercentage; } tex.frameOverTime = new ParticleSystem.MinMaxCurve(1.0f, curve); tex.cycleCount = (int)(cloneTimer.PS.main.startLifetime.constant * 1000 / timeForEmote); } //end of animated emotes //Log("Assigning texture..."); cloneTimer.PSR.material.mainTexture = enhancedImageInfo.Sprite.texture; } else { cloneTimer = ps_Prefab_Pair.Item1[emote.Id]; } cloneTimer.Emit(count); //Log("ParticleSystems notified! "); }