UiAnimator.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.UI
  7. {
  8. [HideMonoScript]
  9. public class UiAnimator : MonoBehaviour
  10. {
  11. [BoxGroup("Settings")] public GameObject objectToAnimate;
  12. [BoxGroup("Settings")] public GameObject objectToDisable;
  13. [BoxGroup("Settings")] public float animationTime = 0.5f;
  14. [BoxGroup("Settings")] public bool animateOnEnable;
  15. [BoxGroup("Settings")] public bool animateOnDisable;
  16. [BoxGroup("Settings"), ShowIf("@animateOnEnable"), LabelText("Animation Delay")] public float delay = 0f;
  17. [BoxGroup("Settings"), ShowIf("@animateOnDisable"), LabelText("Hide Delay")] public float disableDelay = 0f;
  18. [BoxGroup("Settings"), ShowIf("@animateOnDisable")] public bool disableObjectOnEnd = true;
  19. [Tooltip("The UiAnimators on this list will be triggered when this animator is enabled or disabled")]
  20. [BoxGroup("Settings")] public List<UiAnimator> childUiAnimators = new List<UiAnimator>();
  21. [FoldoutGroup("Scale")] public bool animateScale = true;
  22. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public float scaleDuration = 0.3f;
  23. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 sacelFrom;
  24. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 scaleTo;
  25. [FoldoutGroup("Alpha")] public bool animateAlpha = true;
  26. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaDuration = 0.1f;
  27. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaFrom = 0f;
  28. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaTo = 1f;
  29. private LTDescr _tweenObject = new LTDescr();
  30. private bool showing = true;
  31. private bool cancelDisable = false;
  32. public void OnEnable() => Enable();
  33. public void Enable()
  34. {
  35. if(objectToDisable != null)
  36. {
  37. if(objectToDisable.activeSelf == false) objectToDisable.SetActive(true);
  38. }
  39. if(gameObject.activeSelf == false) gameObject.SetActive(true);
  40. if(animateOnEnable)
  41. {
  42. if(!showing) SwapDirection();
  43. cancelDisable = true;
  44. Animate();
  45. }
  46. for (int i = 0; i < childUiAnimators.Count; i++) childUiAnimators[i].Enable();
  47. }
  48. public void Disable()
  49. {
  50. cancelDisable = false;
  51. if(animateOnDisable == false)
  52. {
  53. if(objectToDisable != null) objectToDisable.SetActive(false);
  54. else gameObject.SetActive(false);
  55. return;
  56. }
  57. if(showing) SwapDirection();
  58. Animate();
  59. float totalTime = (delay + animationTime + disableDelay) * 1000;
  60. Timer.ExecuteRealTime(totalTime, () => {
  61. SwapDirection();
  62. if(disableObjectOnEnd && !cancelDisable)
  63. {
  64. if(objectToDisable != null) objectToDisable.SetActive(false);
  65. else if(this != null) gameObject.SetActive(false);
  66. }
  67. });
  68. for (int i = 0; i < childUiAnimators.Count; i++) childUiAnimators[i].Disable();
  69. // _tweenObject.setDelay(disableDelay).setOnComplete(() => {
  70. // SwapDirection();
  71. // if(disableObjectOnEnd && !cancelDisable)
  72. // {
  73. // if(objectToDisable != null) objectToDisable.SetActive(false);
  74. // else gameObject.SetActive(false);
  75. // }
  76. // });
  77. }
  78. private void Animate()
  79. {
  80. //Debug.Log("Animating UI");
  81. if(_tweenObject == null) {
  82. _tweenObject = new LTDescr();
  83. }
  84. if(objectToAnimate == null)
  85. {
  86. objectToAnimate = gameObject;
  87. }
  88. // Scale Animation
  89. if(animateScale)
  90. {
  91. objectToAnimate.GetComponent<RectTransform>().localScale = sacelFrom;
  92. _tweenObject = LeanTween.scale(objectToAnimate, scaleTo, scaleDuration).setIgnoreTimeScale( true ).setDelay(delay);
  93. }
  94. // Fade Animaion
  95. if(animateAlpha)
  96. {
  97. CanvasGroup canvas = objectToAnimate.GetComponent<CanvasGroup>();
  98. if(canvas == null)
  99. {
  100. objectToAnimate.AddComponent<CanvasGroup>();
  101. canvas = objectToAnimate.GetComponent<CanvasGroup>();
  102. }
  103. canvas.alpha = alphaFrom;
  104. _tweenObject = LeanTween.alphaCanvas(canvas, alphaTo, alphaDuration).setIgnoreTimeScale( true ).setDelay(delay);
  105. }
  106. }
  107. private void SwapDirection()
  108. {
  109. var scaleTemp = sacelFrom;
  110. sacelFrom = scaleTo;
  111. scaleTo = scaleTemp;
  112. var alphaTemp = alphaFrom;
  113. alphaFrom = alphaTo;
  114. alphaTo = alphaTemp;
  115. if(showing) showing = false;
  116. else showing = true;
  117. }
  118. }
  119. }