unscaled delta time support

This commit is contained in:
2017-02-11 19:46:49 +07:00
parent 16ee7896bf
commit 290fd066b5
3 changed files with 79 additions and 13 deletions

View File

@@ -26,6 +26,12 @@ namespace FTEditor.Editors {
void DrawControls() { void DrawControls() {
SwfEditorUtils.DoRightHorizontalGUI(() => { 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") ) { if ( _manager.isPaused && GUILayout.Button("Resume") ) {
_manager.Resume(); _manager.Resume();
} }
@@ -44,11 +50,21 @@ namespace FTEditor.Editors {
SwfEditorUtils.DoWithEnabledGUI(false, () => { SwfEditorUtils.DoWithEnabledGUI(false, () => {
EditorGUILayout.TextField("Name", group_name); EditorGUILayout.TextField("Name", group_name);
}); });
EditorGUI.BeginChangeCheck(); {
var new_rate_scale = EditorGUILayout.FloatField( EditorGUI.BeginChangeCheck();
"Rate Scale", _manager.GetGroupRateScale(group_name)); var new_rate_scale = EditorGUILayout.FloatField(
if ( EditorGUI.EndChangeCheck() ) { "Rate Scale", _manager.GetGroupRateScale(group_name));
_manager.SetGroupRateScale(group_name, new_rate_scale); 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(() => { SwfEditorUtils.DoRightHorizontalGUI(() => {
if ( _manager.IsGroupPaused(group_name) && GUILayout.Button("Resume") ) { if ( _manager.IsGroupPaused(group_name) && GUILayout.Button("Resume") ) {

View File

@@ -40,6 +40,9 @@ namespace FTRuntime {
[SerializeField] [SerializeField]
bool _autoPlay = true; bool _autoPlay = true;
[SerializeField]
bool _useUnscaledDt = false;
[SerializeField, SwfFloatRange(0.0f, float.MaxValue)] [SerializeField, SwfFloatRange(0.0f, float.MaxValue)]
float _rateScale = 1.0f; float _rateScale = 1.0f;
@@ -95,6 +98,15 @@ namespace FTRuntime {
set { _autoPlay = value; } set { _autoPlay = value; }
} }
/// <summary>
/// Gets or sets a value indicating whether controller uses unscaled delta time
/// </summary>
/// <value><c>true</c> if uses unscaled delta time; otherwise, <c>false</c></value>
public bool useUnscaledDt {
get { return _useUnscaledDt; }
set { _useUnscaledDt = value; }
}
/// <summary> /// <summary>
/// Gets or sets the controller rate scale /// Gets or sets the controller rate scale
/// </summary> /// </summary>
@@ -296,9 +308,9 @@ namespace FTRuntime {
// //
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
internal void Internal_Update(float dt) { internal void Internal_Update(float scaled_dt, float unscaled_dt) {
if ( isPlaying ) { if ( isPlaying ) {
_tickTimer += dt; _tickTimer += useUnscaledDt ? unscaled_dt : scaled_dt;
do { do {
var frame_rate = clip ? clip.frameRate * rateScale : 0.0f; var frame_rate = clip ? clip.frameRate * rateScale : 0.0f;
var frame_time = frame_rate > 0.0f ? 1.0f / frame_rate : 0.0f; var frame_time = frame_rate > 0.0f ? 1.0f / frame_rate : 0.0f;

View File

@@ -10,8 +10,10 @@ namespace FTRuntime {
SwfList<SwfClipController> _safeUpdates = new SwfList<SwfClipController>(); SwfList<SwfClipController> _safeUpdates = new SwfList<SwfClipController>();
bool _isPaused = false; bool _isPaused = false;
bool _useUnscaledDt = false;
float _rateScale = 1.0f; float _rateScale = 1.0f;
HashSet<string> _groupPauses = new HashSet<string>(); HashSet<string> _groupPauses = new HashSet<string>();
HashSet<string> _groupUnscales = new HashSet<string>();
Dictionary<string, float> _groupRateScales = new Dictionary<string, float>(); Dictionary<string, float> _groupRateScales = new Dictionary<string, float>();
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@@ -78,6 +80,15 @@ namespace FTRuntime {
set { _isPaused = !value; } set { _isPaused = !value; }
} }
/// <summary>
/// Get or set a value indicating whether animation updates uses unscaled delta time
/// </summary>
/// <value><c>true</c> if uses unscaled delta time; otherwise, <c>false</c></value>
public bool useUnscaledDt {
get { return _useUnscaledDt; }
set { _useUnscaledDt = value; }
}
/// <summary> /// <summary>
/// Get or set the global animation rate scale /// Get or set the global animation rate scale
/// </summary> /// </summary>
@@ -146,6 +157,30 @@ namespace FTRuntime {
return !IsGroupPaused(group_name); return !IsGroupPaused(group_name);
} }
/// <summary>
/// Set the group of animations use unscaled delta time
/// </summary>
/// <param name="group_name">Group name</param>
/// <param name="yesno"><c>true</c> if group will use unscaled delta time; otherwise, <c>false</c></param>
public void SetGroupUseUnscaledDt(string group_name, bool yesno) {
if ( !string.IsNullOrEmpty(group_name) ) {
if ( yesno ) {
_groupUnscales.Add(group_name);
} else {
_groupUnscales.Remove(group_name);
}
}
}
/// <summary>
/// Determines whether group of animations uses unscaled delta time
/// </summary>
/// <returns><c>true</c> if group uses unscaled delta time; otherwise, <c>false</c></returns>
/// <param name="group_name">Group name</param>
public bool IsGroupUseUnscaledDt(string group_name) {
return _groupUnscales.Contains(group_name);
}
/// <summary> /// <summary>
/// Set the group of animations rate scale /// Set the group of animations rate scale
/// </summary> /// </summary>
@@ -232,17 +267,19 @@ namespace FTRuntime {
} }
} }
void LateUpdateControllers(float dt) { void LateUpdateControllers(float scaled_dt, float unscaled_dt) {
_controllers.AssignTo(_safeUpdates); _controllers.AssignTo(_safeUpdates);
for ( int i = 0, e = _safeUpdates.Count; i < e; ++i ) { for ( int i = 0, e = _safeUpdates.Count; i < e; ++i ) {
var ctrl = _safeUpdates[i]; var ctrl = _safeUpdates[i];
if ( ctrl ) { if ( ctrl ) {
var group_name = ctrl.groupName; var group_name = ctrl.groupName;
if ( string.IsNullOrEmpty(group_name) ) { if ( string.IsNullOrEmpty(group_name) ) {
ctrl.Internal_Update(dt); ctrl.Internal_Update(scaled_dt, unscaled_dt);
} else if ( IsGroupPlaying(group_name) ) { } else if ( IsGroupPlaying(group_name) ) {
var group_rate_scale = GetGroupRateScale(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() { void LateUpdate() {
if ( isPlaying ) { if ( isPlaying ) {
var dt = Time.deltaTime; LateUpdateControllers(
LateUpdateControllers(rateScale * dt); rateScale * (useUnscaledDt ? Time.unscaledDeltaTime : Time.deltaTime),
rateScale * Time.unscaledDeltaTime);
} }
LateUpdateClips(); LateUpdateClips();
} }