From 26e4dc3a35d9c778684388de1af8b3f288fe627d Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 28 May 2021 20:00:48 +0800 Subject: *Tween --- .../Scripts/Animation/Tween/TweenAnimation.cs | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs') 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 modules; public float duration; @@ -44,6 +56,12 @@ namespace TweenAnimation // 回放次数,0是不限制,默认是0 public int playbackLimit; + // 只触发一次事件 + public bool triggerOnce; + + // 脚本中处理 ITweenEventHandler + public bool scriptHandler; + public List eventList; public TweenAnimation() @@ -52,6 +70,7 @@ namespace TweenAnimation this.playbackLimit = 0; this.duration = 1; this.modules = new List(); + 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(); + eventList.Add(e); + } + + public void RemoveEvent(TweenEvent e) + { + if (eventList == null) + return; + eventList.Remove(e); + } + + /// + /// 根据时间执行tween + /// + /// real time + 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 + } } -- cgit v1.1-26-g67d0