UITweenController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace Core.UI.UTool.UITween
  7. {
  8. [ExecuteInEditMode]
  9. public class UITweenController : MonoBehaviour
  10. {
  11. public List<Object> allTargets = new List<Object>();
  12. public TweenAssetInfo TweenAssetInfo;
  13. public bool TargetFoldout = true;
  14. // public List<TweenEntity> allTweenInfo = new List<TweenEntity>();
  15. public float maxDuration;
  16. public bool isUpdate;
  17. [HideInInspector] public float currTime;
  18. private void OnEnable()
  19. {
  20. if (Application.isPlaying)
  21. {
  22. StartPlay();
  23. }
  24. }
  25. public void StartPlay()
  26. {
  27. if (TweenAssetInfo == null)
  28. {
  29. return;
  30. }
  31. maxDuration = 0;
  32. for (int i = 0; i < TweenAssetInfo.allTweenInfo.Count; i++)
  33. {
  34. TweenEntity tweenEntity = TweenAssetInfo.allTweenInfo[i];
  35. if (tweenEntity.duration + tweenEntity.delay > maxDuration)
  36. {
  37. maxDuration = tweenEntity.duration + tweenEntity.delay;
  38. }
  39. tweenEntity.Prepare();
  40. tweenEntity.Rest(allTargets[i]);
  41. }
  42. isUpdate = true;
  43. currTime = 0;
  44. }
  45. public void SetMaxTime()
  46. {
  47. maxDuration = 0;
  48. for (int i = 0; i < TweenAssetInfo.allTweenInfo.Count; i++)
  49. {
  50. TweenEntity tweenEntity = TweenAssetInfo.allTweenInfo[i];
  51. if (tweenEntity.duration + tweenEntity.delay > maxDuration)
  52. {
  53. maxDuration = tweenEntity.duration + tweenEntity.delay;
  54. }
  55. }
  56. }
  57. public void JumpToTime(float t)
  58. {
  59. for (int i = 0; i < TweenAssetInfo.allTweenInfo.Count; i++)
  60. {
  61. TweenAssetInfo.allTweenInfo[i].Prepare();
  62. // allTweenInfo[i].Rest();
  63. }
  64. PlayTween(t, true);
  65. }
  66. private void PlayTween(float t, bool isFallBack)
  67. {
  68. for (int i = 0; i < TweenAssetInfo.allTweenInfo.Count; i++)
  69. {
  70. TweenAssetInfo.allTweenInfo[i].Play(allTargets[i], t, isFallBack);
  71. }
  72. }
  73. private void Update()
  74. {
  75. if (!isUpdate)
  76. {
  77. return;
  78. }
  79. currTime += Time.deltaTime;
  80. PlayTween(currTime, false);
  81. if (currTime >= maxDuration)
  82. {
  83. isUpdate = false;
  84. }
  85. }
  86. }
  87. }