123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- namespace KairoEngine.UI
- {
- [HideMonoScript]
- public class UiAnimator : MonoBehaviour
- {
- [BoxGroup("Settings")] public GameObject objectToAnimate;
- [BoxGroup("Settings")] public GameObject objectToDisable;
- [BoxGroup("Settings")] public float animationTime = 0.5f;
- [BoxGroup("Settings")] public bool animateOnEnable;
- [BoxGroup("Settings")] public bool animateOnDisable;
-
- [BoxGroup("Settings"), ShowIf("@animateOnEnable"), LabelText("Animation Delay")] public float delay = 0f;
- [BoxGroup("Settings"), ShowIf("@animateOnDisable"), LabelText("Hide Delay")] public float disableDelay = 0f;
-
-
- [BoxGroup("Settings"), ShowIf("@animateOnDisable")] public bool disableObjectOnEnd = true;
- [Tooltip("The UiAnimators on this list will be triggered when this animator is enabled or disabled")]
- [BoxGroup("Settings")] public List<UiAnimator> childUiAnimators = new List<UiAnimator>();
- [FoldoutGroup("Scale")] public bool animateScale = true;
- [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public float scaleDuration = 0.3f;
- [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 sacelFrom;
- [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 scaleTo;
- [FoldoutGroup("Alpha")] public bool animateAlpha = true;
- [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaDuration = 0.1f;
- [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaFrom = 0f;
- [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaTo = 1f;
-
- private LTDescr _tweenObject = new LTDescr();
- private bool showing = true;
- private bool cancelDisable = false;
- public void OnEnable() => Enable();
- public void Enable()
- {
- if(objectToDisable != null)
- {
- if(objectToDisable.activeSelf == false) objectToDisable.SetActive(true);
- }
- if(gameObject.activeSelf == false) gameObject.SetActive(true);
- if(animateOnEnable)
- {
- if(!showing) SwapDirection();
- cancelDisable = true;
- Animate();
- }
- for (int i = 0; i < childUiAnimators.Count; i++) childUiAnimators[i].Enable();
- }
- public void Disable()
- {
- cancelDisable = false;
- if(animateOnDisable == false)
- {
- if(objectToDisable != null) objectToDisable.SetActive(false);
- else gameObject.SetActive(false);
- return;
- }
- if(showing) SwapDirection();
- Animate();
- float totalTime = (delay + animationTime + disableDelay) * 1000;
- Timer.ExecuteRealTime(totalTime, () => {
- SwapDirection();
- if(disableObjectOnEnd && !cancelDisable)
- {
- if(objectToDisable != null) objectToDisable.SetActive(false);
- else if(this != null) gameObject.SetActive(false);
- }
- });
- for (int i = 0; i < childUiAnimators.Count; i++) childUiAnimators[i].Disable();
- // _tweenObject.setDelay(disableDelay).setOnComplete(() => {
- // SwapDirection();
- // if(disableObjectOnEnd && !cancelDisable)
- // {
- // if(objectToDisable != null) objectToDisable.SetActive(false);
- // else 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;
- if(showing) showing = false;
- else showing = true;
- }
- }
- }
|