private void OnEnable() { unit = this.GetComponent <EnemyUnit>(); rebirthAction = this.GetComponentInChildren <IncarnateRebirthAttackAction>(); animationStateUpdater = this.GetComponentInChildren <AnimationStateUpdater>(); EnemyUnit.EnemyWithTypeDied += EnemyDied; }
/// <summary> /// Sets an image or gif/apng from a resource path /// </summary> /// <param name="image">Image component to set the image to</param> /// <param name="location">Resource path, file path, or url of image. Can prefix with # to find and use a base game sprite. May need to prefix resource paths with 'AssemblyName:'</param> public static void SetImage(this Image image, string location) { AnimationStateUpdater oldStateUpdater = image.GetComponent <AnimationStateUpdater>(); if (oldStateUpdater != null) { MonoBehaviour.DestroyImmediate(oldStateUpdater); } bool isURL = Uri.TryCreate(location, UriKind.Absolute, out Uri uri); if (location.StartsWith("#")) { string imgName = location.Substring(1); try { image.sprite = Resources.FindObjectsOfTypeAll <Sprite>().First(x => x.name == imgName); } catch { Logger.log.Error($"Could not find Texture with image name {imgName}"); } } else if (IsAnimated(location) || (isURL && IsAnimated(uri.LocalPath))) { AnimationStateUpdater stateUpdater = image.gameObject.AddComponent <AnimationStateUpdater>(); stateUpdater.image = image; stateUpdater.controllerData = AnimationController.instance.loadingAnimation; if (AnimationController.instance.RegisteredAnimations.TryGetValue(location, out AnimationControllerData animControllerData)) { stateUpdater.controllerData = animControllerData; } else { Utilities.GetData(location, (byte[] data) => { AnimationLoader.Process((location.EndsWith(".gif") || (isURL && uri.LocalPath.EndsWith(".gif"))) ? AnimationType.GIF : AnimationType.APNG, data, (Texture2D tex, Rect[] uvs, float[] delays, int width, int height) => { AnimationControllerData controllerData = AnimationController.instance.Register(location, tex, uvs, delays); stateUpdater.controllerData = controllerData; }); }); } } else { AnimationStateUpdater stateUpdater = image.gameObject.AddComponent <AnimationStateUpdater>(); stateUpdater.image = image; stateUpdater.controllerData = AnimationController.instance.loadingAnimation; Utilities.GetData(location, (byte[] data) => { if (stateUpdater != null) { GameObject.DestroyImmediate(stateUpdater); } image.sprite = Utilities.LoadSpriteRaw(data); image.sprite.texture.wrapMode = TextureWrapMode.Clamp; }); } }
/// <summary> /// Sets an image or gif/apng from a resource path /// </summary> /// <param name="image">Image component to set the image to</param> /// <param name="location">Resource path, file path, or url of image. Can prefix with # to find and use a base game sprite. May need to prefix resource paths with 'AssemblyName:'</param> /// <param name="loadingAnimation">Whether a loading animation is shown as a placeholder until the image is loaded</param> /// <param name="scaleOptions">If the image should be downscaled and what it should be downscaled to</param> /// <param name="callback">Method to call once SetImage has finished</param> public static void SetImage(this Image image, string location, bool loadingAnimation, ScaleOptions scaleOptions, Action callback) { AnimationStateUpdater oldStateUpdater = image.GetComponent <AnimationStateUpdater>(); if (oldStateUpdater != null) { MonoBehaviour.DestroyImmediate(oldStateUpdater); } if (location.Length > 1 && location[0] == '#') { string imgName = location.Substring(1); image.sprite = Utilities.FindSpriteCached(imgName); if (image.sprite == null) { Logger.log.Error($"Could not find Sprite with image name {imgName}"); } return; } bool isURL = Uri.TryCreate(location, UriKind.Absolute, out Uri uri); if (IsAnimated(location) || isURL && IsAnimated(uri.LocalPath)) { AnimationStateUpdater stateUpdater = image.gameObject.AddComponent <AnimationStateUpdater>(); stateUpdater.image = image; if (loadingAnimation) { stateUpdater.controllerData = AnimationController.instance.loadingAnimation; } if (AnimationController.instance.RegisteredAnimations.TryGetValue(location, out AnimationControllerData animControllerData)) { stateUpdater.controllerData = animControllerData; } else { Utilities.GetData(location, (byte[] data) => { AnimationLoader.Process( (location.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) || (isURL && uri.LocalPath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))) ? AnimationType.GIF : AnimationType.APNG, data, (Texture2D tex, Rect[] uvs, float[] delays, int width, int height) => { AnimationControllerData controllerData = AnimationController.instance.Register(location, tex, uvs, delays); stateUpdater.controllerData = controllerData; callback?.Invoke(); }); }); return; } } else { AnimationStateUpdater stateUpdater = image.gameObject.AddComponent <AnimationStateUpdater>(); stateUpdater.image = image; if (loadingAnimation) { stateUpdater.controllerData = AnimationController.instance.loadingAnimation; } Utilities.GetData(location, async(byte[] data) => { if (stateUpdater != null) { GameObject.DestroyImmediate(stateUpdater); } if (scaleOptions.ShouldScale) { var imageBytes = await Task.Run(() => DownScaleImage(data, scaleOptions)).ConfigureAwait(false); _ = UnityMainThreadTaskScheduler.Factory.StartNew(() => { image.sprite = Utilities.LoadSpriteRaw(imageBytes); image.sprite.texture.wrapMode = TextureWrapMode.Clamp; }); } else { image.sprite = Utilities.LoadSpriteRaw(data); image.sprite.texture.wrapMode = TextureWrapMode.Clamp; } callback?.Invoke(); }); return; } callback?.Invoke(); }