summaryrefslogtreecommitdiff
path: root/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-05-28 20:00:48 +0800
committerchai <chaifix@163.com>2021-05-28 20:00:48 +0800
commit26e4dc3a35d9c778684388de1af8b3f288fe627d (patch)
treee791e224c2be0dcda69dda2211d213f946b3b2cc /Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
parent3bf862ea76d349c2aec1f71656dd6f41a82650e0 (diff)
*Tween
Diffstat (limited to 'Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs')
-rw-r--r--Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs94
1 files changed, 94 insertions, 0 deletions
diff --git a/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs b/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
index ccefe06..e722a08 100644
--- a/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
+++ b/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
@@ -13,6 +13,17 @@ namespace TweenAnimation
public string name; // 事件名
public float time; // 事件触发时间
public UnityEvent eventHandler; // 回调函数
+#if UNITY_EDITOR
+ [NonSerialized]
+ public bool unfold;
+#endif
+ public TweenEvent()
+ {
+ unfold = false;
+ name = "New Event";
+ time = 0;
+ eventHandler = null;
+ }
}
public class TweenAnimation : MonoBehaviour
@@ -33,6 +44,7 @@ namespace TweenAnimation
public string description = "No Description";
+ [SerializeReference]
public List<TweenModule> modules;
public float duration;
@@ -44,6 +56,12 @@ namespace TweenAnimation
// 回放次数,0是不限制,默认是0
public int playbackLimit;
+ // 只触发一次事件
+ public bool triggerOnce;
+
+ // 脚本中处理 ITweenEventHandler
+ public bool scriptHandler;
+
public List<TweenEvent> eventList;
public TweenAnimation()
@@ -52,6 +70,7 @@ namespace TweenAnimation
this.playbackLimit = 0;
this.duration = 1;
this.modules = new List<TweenModule>();
+ this.triggerOnce = false;
}
public void AddModule(TweenModule module)
@@ -69,6 +88,81 @@ namespace TweenAnimation
modules.Remove(module);
}
+ public void AddEvent(TweenEvent e)
+ {
+ if (eventList == null)
+ eventList = new List<TweenEvent>();
+ eventList.Add(e);
+ }
+
+ public void RemoveEvent(TweenEvent e)
+ {
+ if (eventList == null)
+ return;
+ eventList.Remove(e);
+ }
+
+ /// <summary>
+ /// 根据时间执行tween
+ /// </summary>
+ /// <param name="time">real time</param>
+ public void Process(float time)
+ {
+ if (modules == null)
+ return;
+
+ float logicTime = time;
+
+ if (this.playbackStyle == PlaybackStyle.Once)
+ logicTime = HandleOnce(time);
+ else if (this.playbackStyle == PlaybackStyle.Loop)
+ logicTime = HandleLoop(time);
+ else if (this.playbackStyle == PlaybackStyle.PingPong)
+ logicTime = HandlePingpong(time);
+
+ for (int i = 0; i < modules.Count; i ++)
+ {
+ TweenModule module = modules[i];
+ if (!module.enabled)
+ continue;
+ module.OnUpdate(logicTime);
+ }
+ }
+
+ private float HandleOnce(float time)
+ {
+ time = Mathf.Clamp(time, 0, duration);
+ return time;
+ }
+
+ private float HandleLoop(float time)
+ {
+ time = time % duration;
+ return time;
+ }
+
+ private float HandlePingpong(float time)
+ {
+ if (time <= duration)
+ return time;
+ float remain = time % duration;
+ int round = (int)(time / duration);
+ bool bOdd = (round & 1) == 1;
+ if (bOdd)
+ time = duration - remain;
+ else
+ time = remain;
+
+ return time;
+ }
+
+#if UNITY_EDITOR
+ public void UpdateEditor(float time)
+ {
+ Process(time);
+ }
+#endif
+
}
}