123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<RectTransform>().localScale = sacelFrom;
- _tweenObject = LeanTween.scale(objectToAnimate, scaleTo, scaleDuration).setIgnoreTimeScale( true ).setDelay(delay);
- }
- // Fade Animaion
- if(animateAlpha)
- {
- CanvasGroup canvas = objectToAnimate.GetComponent<CanvasGroup>();
- if(canvas == null)
- {
- objectToAnimate.AddComponent<CanvasGroup>();
- canvas = objectToAnimate.GetComponent<CanvasGroup>();
- }
- 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;
- }
- }
- }
|