diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index 4fbc053a..e14e9c09 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -79,6 +79,7 @@ slug: /changelog ## master +* (2025.03.07) 增加`Animation`的`speed`可指定动画速度 * (2025.03.06) 优化`Animation`的新增动画表现 * (2025.03.04) 修复`Treemap`的`label`显示异常的问题 * (2025.03.02) 增加`Tooltip`的`columnGapWidths`参数设置列文本间隙距离 diff --git a/Editor/ChildComponents/AnimationDrawer.cs b/Editor/ChildComponents/AnimationDrawer.cs index 4e28aa5d..ac09c04d 100644 --- a/Editor/ChildComponents/AnimationDrawer.cs +++ b/Editor/ChildComponents/AnimationDrawer.cs @@ -15,6 +15,7 @@ namespace XCharts.Editor ++EditorGUI.indentLevel; PropertyField(prop, "m_Delay"); PropertyField(prop, "m_Duration"); + PropertyField(prop, "m_Speed"); --EditorGUI.indentLevel; } } @@ -30,6 +31,7 @@ namespace XCharts.Editor { ++EditorGUI.indentLevel; PropertyField(prop, "m_Duration"); + PropertyField(prop, "m_Speed"); --EditorGUI.indentLevel; } } @@ -45,6 +47,7 @@ namespace XCharts.Editor { ++EditorGUI.indentLevel; PropertyField(prop, "m_Duration"); + PropertyField(prop, "m_Speed"); --EditorGUI.indentLevel; } } diff --git a/Runtime/Component/Animation/AnimationInfo.cs b/Runtime/Component/Animation/AnimationInfo.cs index fecc4cd9..d87c7d0e 100644 --- a/Runtime/Component/Animation/AnimationInfo.cs +++ b/Runtime/Component/Animation/AnimationInfo.cs @@ -16,6 +16,7 @@ namespace XCharts.Runtime [SerializeField][Since("v3.8.0")] private bool m_Reverse = false; [SerializeField][Since("v3.8.0")] private float m_Delay = 0; [SerializeField][Since("v3.8.0")] private float m_Duration = 1000; + [SerializeField][Since("v3.14.0")] private float m_Speed = 0; public AnimationInfoContext context = new AnimationInfoContext(); /// @@ -34,11 +35,15 @@ namespace XCharts.Runtime /// public float delay { get { return m_Delay; } set { m_Delay = value; } } /// - /// the duration of animation. - /// ||动画的时长。 + /// the duration of animation. Default is used to calculate the speed of animation. It can also be specified by speed. + /// ||动画的时长。默认用于计算动画的速度。也可以通过speed指定速度。 /// public float duration { get { return m_Duration; } set { m_Duration = value; } } - + /// + /// the speed of animation. When speed is specified, duration will be invalid. Default is 0, which means no speed specified. + /// ||动画的速度。当指定speed时,duration将失效。默认为0,表示不指定速度。 + /// + public float speed { get { return m_Speed; } set { m_Speed = value; } } /// /// the callback function of animation start. /// ||动画开始的回调。 @@ -208,7 +213,7 @@ namespace XCharts.Runtime if (!context.start) return false; else - return (m_Delay > 0 && Time.time - context.startTime < m_Delay / 1000); + return m_Delay > 0 && Time.time - context.startTime < m_Delay / 1000; } /// @@ -285,8 +290,7 @@ namespace XCharts.Runtime { if (!context.start || !context.init || context.pause) return; if (IsInDelay()) return; - var duration = GetCurrAnimationDuration(); - var delta = (float)(total / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime)); + var delta = GetDelta(total, m_UnscaledTime); if (reverse) { context.currProgress -= delta; @@ -330,8 +334,7 @@ namespace XCharts.Runtime } else { - var duration = GetCurrAnimationDuration(dataIndex); - var delta = (destProgress - startProgress) / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime); + var delta = GetDelta(destProgress - startProgress, m_UnscaledTime); currHig += delta; if (reverse) { @@ -362,8 +365,7 @@ namespace XCharts.Runtime if (IsInDelay()) return; - var duration = GetCurrAnimationDuration(); - var delta = dest / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime); + var delta = GetDelta(dest, m_UnscaledTime); if (reverse) { context.sizeProgress -= delta; @@ -377,6 +379,20 @@ namespace XCharts.Runtime context.sizeProgress = dest; } } + + private float GetDelta(double total, bool unscaledTime) + { + if (m_Speed > 0) + { + context.currDuration = (float)total / m_Speed; + return (float)(m_Speed * (unscaledTime ? Time.unscaledDeltaTime : Time.deltaTime)); + } + else + { + context.currDuration = 0; + return (float)(total / GetCurrAnimationDuration() * (unscaledTime ? Time.unscaledDeltaTime : Time.deltaTime)); + } + } } /// diff --git a/Runtime/Component/Animation/AnimationInfoContext.cs b/Runtime/Component/Animation/AnimationInfoContext.cs index b524e63e..1ff1bef6 100644 --- a/Runtime/Component/Animation/AnimationInfoContext.cs +++ b/Runtime/Component/Animation/AnimationInfoContext.cs @@ -17,6 +17,7 @@ namespace XCharts.Runtime public float sizeProgress; public int currPointIndex; public int destPointIndex; + public float currDuration; public Vector3 currPoint; public Vector3 destPoint; public Dictionary dataCurrProgress = new Dictionary(); diff --git a/Runtime/Component/Animation/AnimationStyle.cs b/Runtime/Component/Animation/AnimationStyle.cs index 883af7da..a14980c9 100644 --- a/Runtime/Component/Animation/AnimationStyle.cs +++ b/Runtime/Component/Animation/AnimationStyle.cs @@ -560,7 +560,7 @@ namespace XCharts.Runtime public float GetChangeDuration() { if (m_Enable && m_Change.enable) - return m_Change.duration; + return m_Change.context.currDuration > 0 ? m_Change.context.currDuration : m_Change.duration; else return 0; } @@ -568,7 +568,7 @@ namespace XCharts.Runtime public float GetAdditionDuration() { if (m_Enable && m_Addition.enable) - return m_Addition.duration; + return m_Addition.context.currDuration > 0 ? m_Addition.context.currDuration : m_Addition.duration; else return 0; } @@ -576,7 +576,7 @@ namespace XCharts.Runtime public float GetInteractionDuration() { if (m_Enable && m_Interaction.enable) - return m_Interaction.duration; + return m_Interaction.context.currDuration > 0 ? m_Interaction.context.currDuration : m_Interaction.duration; else return 0; }