From 290fd066b5eb9b5111a09733ca7cf1eff10c091c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 11 Feb 2017 19:46:49 +0700 Subject: [PATCH] unscaled delta time support --- .../FTEditor/Editors/SwfManagerEditor.cs | 26 ++++++++-- .../Scripts/FTRuntime/SwfClipController.cs | 16 +++++- .../Scripts/FTRuntime/SwfManager.cs | 50 ++++++++++++++++--- 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/Assets/FlashTools/Scripts/Editor/FTEditor/Editors/SwfManagerEditor.cs b/Assets/FlashTools/Scripts/Editor/FTEditor/Editors/SwfManagerEditor.cs index e386d59..abf0f7f 100644 --- a/Assets/FlashTools/Scripts/Editor/FTEditor/Editors/SwfManagerEditor.cs +++ b/Assets/FlashTools/Scripts/Editor/FTEditor/Editors/SwfManagerEditor.cs @@ -26,6 +26,12 @@ namespace FTEditor.Editors { void DrawControls() { SwfEditorUtils.DoRightHorizontalGUI(() => { + if ( _manager.useUnscaledDt && GUILayout.Button("Use Scaled Dt") ) { + _manager.useUnscaledDt = false; + } + if ( !_manager.useUnscaledDt && GUILayout.Button("Use Unscaled Dt") ) { + _manager.useUnscaledDt = true; + } if ( _manager.isPaused && GUILayout.Button("Resume") ) { _manager.Resume(); } @@ -44,11 +50,21 @@ namespace FTEditor.Editors { SwfEditorUtils.DoWithEnabledGUI(false, () => { EditorGUILayout.TextField("Name", group_name); }); - EditorGUI.BeginChangeCheck(); - var new_rate_scale = EditorGUILayout.FloatField( - "Rate Scale", _manager.GetGroupRateScale(group_name)); - if ( EditorGUI.EndChangeCheck() ) { - _manager.SetGroupRateScale(group_name, new_rate_scale); + { + EditorGUI.BeginChangeCheck(); + var new_rate_scale = EditorGUILayout.FloatField( + "Rate Scale", _manager.GetGroupRateScale(group_name)); + if ( EditorGUI.EndChangeCheck() ) { + _manager.SetGroupRateScale(group_name, new_rate_scale); + } + } + { + EditorGUI.BeginChangeCheck(); + var new_user_unscaled_dt = EditorGUILayout.Toggle( + "Use Unscaled Dt", _manager.IsGroupUseUnscaledDt(group_name)); + if ( EditorGUI.EndChangeCheck() ) { + _manager.SetGroupUseUnscaledDt(group_name, new_user_unscaled_dt); + } } SwfEditorUtils.DoRightHorizontalGUI(() => { if ( _manager.IsGroupPaused(group_name) && GUILayout.Button("Resume") ) { diff --git a/Assets/FlashTools/Scripts/FTRuntime/SwfClipController.cs b/Assets/FlashTools/Scripts/FTRuntime/SwfClipController.cs index 88bec50..dcb43e1 100644 --- a/Assets/FlashTools/Scripts/FTRuntime/SwfClipController.cs +++ b/Assets/FlashTools/Scripts/FTRuntime/SwfClipController.cs @@ -40,6 +40,9 @@ namespace FTRuntime { [SerializeField] bool _autoPlay = true; + [SerializeField] + bool _useUnscaledDt = false; + [SerializeField, SwfFloatRange(0.0f, float.MaxValue)] float _rateScale = 1.0f; @@ -95,6 +98,15 @@ namespace FTRuntime { set { _autoPlay = value; } } + /// + /// Gets or sets a value indicating whether controller uses unscaled delta time + /// + /// true if uses unscaled delta time; otherwise, false + public bool useUnscaledDt { + get { return _useUnscaledDt; } + set { _useUnscaledDt = value; } + } + /// /// Gets or sets the controller rate scale /// @@ -296,9 +308,9 @@ namespace FTRuntime { // // --------------------------------------------------------------------- - internal void Internal_Update(float dt) { + internal void Internal_Update(float scaled_dt, float unscaled_dt) { if ( isPlaying ) { - _tickTimer += dt; + _tickTimer += useUnscaledDt ? unscaled_dt : scaled_dt; do { var frame_rate = clip ? clip.frameRate * rateScale : 0.0f; var frame_time = frame_rate > 0.0f ? 1.0f / frame_rate : 0.0f; diff --git a/Assets/FlashTools/Scripts/FTRuntime/SwfManager.cs b/Assets/FlashTools/Scripts/FTRuntime/SwfManager.cs index 63a635f..e54f766 100644 --- a/Assets/FlashTools/Scripts/FTRuntime/SwfManager.cs +++ b/Assets/FlashTools/Scripts/FTRuntime/SwfManager.cs @@ -10,8 +10,10 @@ namespace FTRuntime { SwfList _safeUpdates = new SwfList(); bool _isPaused = false; + bool _useUnscaledDt = false; float _rateScale = 1.0f; HashSet _groupPauses = new HashSet(); + HashSet _groupUnscales = new HashSet(); Dictionary _groupRateScales = new Dictionary(); // --------------------------------------------------------------------- @@ -78,6 +80,15 @@ namespace FTRuntime { set { _isPaused = !value; } } + /// + /// Get or set a value indicating whether animation updates uses unscaled delta time + /// + /// true if uses unscaled delta time; otherwise, false + public bool useUnscaledDt { + get { return _useUnscaledDt; } + set { _useUnscaledDt = value; } + } + /// /// Get or set the global animation rate scale /// @@ -146,6 +157,30 @@ namespace FTRuntime { return !IsGroupPaused(group_name); } + /// + /// Set the group of animations use unscaled delta time + /// + /// Group name + /// true if group will use unscaled delta time; otherwise, false + public void SetGroupUseUnscaledDt(string group_name, bool yesno) { + if ( !string.IsNullOrEmpty(group_name) ) { + if ( yesno ) { + _groupUnscales.Add(group_name); + } else { + _groupUnscales.Remove(group_name); + } + } + } + + /// + /// Determines whether group of animations uses unscaled delta time + /// + /// true if group uses unscaled delta time; otherwise, false + /// Group name + public bool IsGroupUseUnscaledDt(string group_name) { + return _groupUnscales.Contains(group_name); + } + /// /// Set the group of animations rate scale /// @@ -232,17 +267,19 @@ namespace FTRuntime { } } - void LateUpdateControllers(float dt) { + void LateUpdateControllers(float scaled_dt, float unscaled_dt) { _controllers.AssignTo(_safeUpdates); for ( int i = 0, e = _safeUpdates.Count; i < e; ++i ) { var ctrl = _safeUpdates[i]; if ( ctrl ) { - var group_name = ctrl.groupName; + var group_name = ctrl.groupName; if ( string.IsNullOrEmpty(group_name) ) { - ctrl.Internal_Update(dt); + ctrl.Internal_Update(scaled_dt, unscaled_dt); } else if ( IsGroupPlaying(group_name) ) { var group_rate_scale = GetGroupRateScale(group_name); - ctrl.Internal_Update(group_rate_scale * dt); + ctrl.Internal_Update( + group_rate_scale * (IsGroupUseUnscaledDt(group_name) ? unscaled_dt : scaled_dt), + group_rate_scale * unscaled_dt); } } } @@ -267,8 +304,9 @@ namespace FTRuntime { void LateUpdate() { if ( isPlaying ) { - var dt = Time.deltaTime; - LateUpdateControllers(rateScale * dt); + LateUpdateControllers( + rateScale * (useUnscaledDt ? Time.unscaledDeltaTime : Time.deltaTime), + rateScale * Time.unscaledDeltaTime); } LateUpdateClips(); }