using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; namespace KairoEngine.UI { public class UiAnimator : MonoBehaviour { public GameObject objectToAnimate; public float delay = 0f; public float disableDelay = 0f; public bool animateOnEnable; public bool animateOnDisable; public bool disableObjectOnEnd = true; public bool animateScale = true; [ShowIf(@"animateScale")] public float scaleDuration = 0.3f; [ShowIf(@"animateScale")] public Vector3 sacelFrom; [ShowIf(@"animateScale")] public Vector3 scaleTo; public bool animateAlpha = true; [ShowIf(@"animateAlpha")] public float alphaDuration = 0.1f; [ShowIf(@"animateAlpha")] public float alphaFrom = 0f; [ShowIf(@"animateAlpha")] public float alphaTo = 1f; private LTDescr _tweenObject = new LTDescr(); public void OnEnable() { if(animateOnEnable) { Animate(); } } public void Disable() { if(animateOnDisable == false) { if(disableObjectOnEnd) gameObject.SetActive(false); return; } SwapDirection(); Animate(); _tweenObject.setDelay(disableDelay).setOnComplete(() => { SwapDirection(); if(disableObjectOnEnd) gameObject.SetActive(false); }); } private void Animate() { //Debug.Log("Animating UI"); if(_tweenObject == null) { _tweenObject = new LTDescr(); } if(objectToAnimate == null) { objectToAnimate = gameObject; } // Scale Animation if(animateScale) { objectToAnimate.GetComponent().localScale = sacelFrom; _tweenObject = LeanTween.scale(objectToAnimate, scaleTo, scaleDuration).setIgnoreTimeScale( true ).setDelay(delay); } // Fade Animaion if(animateAlpha) { CanvasGroup canvas = objectToAnimate.GetComponent(); if(canvas == null) { objectToAnimate.AddComponent(); canvas = objectToAnimate.GetComponent(); } canvas.alpha = alphaFrom; _tweenObject = LeanTween.alphaCanvas(canvas, alphaTo, alphaDuration).setIgnoreTimeScale( true ).setDelay(delay); } } private void SwapDirection() { var scaleTemp = sacelFrom; sacelFrom = scaleTo; scaleTo = scaleTemp; var alphaTemp = alphaFrom; alphaFrom = alphaTo; alphaTo = alphaTemp; } } }