UiAnimator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. [FoldoutGroup("Scale")] public bool animateScale = true;
  20. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public float scaleDuration = 0.3f;
  21. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 sacelFrom;
  22. [FoldoutGroup("Scale"), ShowIf(@"animateScale")] public Vector3 scaleTo;
  23. [FoldoutGroup("Alpha")] public bool animateAlpha = true;
  24. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaDuration = 0.1f;
  25. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaFrom = 0f;
  26. [FoldoutGroup("Alpha"), ShowIf(@"animateAlpha")] public float alphaTo = 1f;
  27. private LTDescr _tweenObject = new LTDescr();
  28. private bool showing = true;
  29. private bool cancelDisable = false;
  30. public void OnEnable() => Enable();
  31. public void Enable()
  32. {
  33. if(objectToDisable != null)
  34. {
  35. if(objectToDisable.activeSelf == false) objectToDisable.SetActive(true);
  36. }
  37. if(gameObject.activeSelf == false) gameObject.SetActive(true);
  38. if(animateOnEnable)
  39. {
  40. if(!showing) SwapDirection();
  41. cancelDisable = true;
  42. Animate();
  43. }
  44. }
  45. public void Disable()
  46. {
  47. cancelDisable = false;
  48. if(animateOnDisable == false)
  49. {
  50. if(objectToDisable != null) objectToDisable.SetActive(false);
  51. else gameObject.SetActive(false);
  52. return;
  53. }
  54. if(showing) SwapDirection();
  55. Animate();
  56. float totalTime = (delay + animationTime + disableDelay) * 1000;
  57. Timer.Execute(totalTime, () => {
  58. SwapDirection();
  59. if(disableObjectOnEnd && !cancelDisable)
  60. {
  61. if(objectToDisable != null) objectToDisable.SetActive(false);
  62. else gameObject.SetActive(false);
  63. }
  64. });
  65. // _tweenObject.setDelay(disableDelay).setOnComplete(() => {
  66. // SwapDirection();
  67. // if(disableObjectOnEnd && !cancelDisable)
  68. // {
  69. // if(objectToDisable != null) objectToDisable.SetActive(false);
  70. // else gameObject.SetActive(false);
  71. // }
  72. // });
  73. }
  74. private void Animate()
  75. {
  76. //Debug.Log("Animating UI");
  77. if(_tweenObject == null) {
  78. _tweenObject = new LTDescr();
  79. }
  80. if(objectToAnimate == null)
  81. {
  82. objectToAnimate = gameObject;
  83. }
  84. // Scale Animation
  85. if(animateScale)
  86. {
  87. objectToAnimate.GetComponent<RectTransform>().localScale = sacelFrom;
  88. _tweenObject = LeanTween.scale(objectToAnimate, scaleTo, scaleDuration).setIgnoreTimeScale( true ).setDelay(delay);
  89. }
  90. // Fade Animaion
  91. if(animateAlpha)
  92. {
  93. CanvasGroup canvas = objectToAnimate.GetComponent<CanvasGroup>();
  94. if(canvas == null)
  95. {
  96. objectToAnimate.AddComponent<CanvasGroup>();
  97. canvas = objectToAnimate.GetComponent<CanvasGroup>();
  98. }
  99. canvas.alpha = alphaFrom;
  100. _tweenObject = LeanTween.alphaCanvas(canvas, alphaTo, alphaDuration).setIgnoreTimeScale( true ).setDelay(delay);
  101. }
  102. }
  103. private void SwapDirection()
  104. {
  105. var scaleTemp = sacelFrom;
  106. sacelFrom = scaleTo;
  107. scaleTo = scaleTemp;
  108. var alphaTemp = alphaFrom;
  109. alphaFrom = alphaTo;
  110. alphaTo = alphaTemp;
  111. if(showing) showing = false;
  112. else showing = true;
  113. }
  114. }
  115. }