-
-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #87; Use Canvas.willRenderCanvases event instead of Update method
- Loading branch information
Showing
4 changed files
with
179 additions
and
96 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using UnityEngine; | ||
using System; | ||
|
||
namespace Coffee.UIExtensions | ||
{ | ||
/// <summary> | ||
/// Effect Runner. | ||
/// </summary> | ||
[Serializable] | ||
public class EffectRunner | ||
{ | ||
//################################ | ||
// Public Members. | ||
//################################ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether is running. | ||
/// </summary> | ||
public bool running;// { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether can loop. | ||
/// </summary> | ||
public bool loop;// { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the duration. | ||
/// </summary> | ||
public float duration;// { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the delay before looping. | ||
/// </summary> | ||
public float loopDelay;// { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the update mode. | ||
/// </summary> | ||
public AnimatorUpdateMode updateMode;// { get; set; } | ||
|
||
// /// <summary> | ||
// /// Register runner. | ||
// /// </summary> | ||
// public void Register(bool running,float duration, AnimatorUpdateMode updateMode, bool loop, float loopDelay, Action<float> callback) | ||
// { | ||
// Canvas.willRenderCanvases += OnWillRenderCanvases; | ||
// | ||
// _time = 0; | ||
// this._callback = callback; | ||
// this.loop = loop; | ||
// this.running = running; | ||
// this.duration = duration; | ||
// this.loopDelay = loopDelay; | ||
// this.updateMode = updateMode; | ||
// } | ||
|
||
public void OnEnable(Action<float> callback) | ||
{ | ||
Canvas.willRenderCanvases += OnWillRenderCanvases; | ||
|
||
_time = 0; | ||
this._callback = callback; | ||
} | ||
|
||
// /// <summary> | ||
// /// Unregister runner. | ||
// /// </summary> | ||
// public void Unregister() | ||
// { | ||
// _callback = null; | ||
// Canvas.willRenderCanvases -= OnWillRenderCanvases; | ||
// } | ||
|
||
/// <summary> | ||
/// Unregister runner. | ||
/// </summary> | ||
public void OnDisable() | ||
{ | ||
_callback = null; | ||
Canvas.willRenderCanvases -= OnWillRenderCanvases; | ||
} | ||
|
||
/// <summary> | ||
/// Play runner. | ||
/// </summary> | ||
public void Play() | ||
{ | ||
_time = 0; | ||
running = true; | ||
} | ||
|
||
//################################ | ||
// Private Members. | ||
//################################ | ||
float _time = 0; | ||
Action<float> _callback; | ||
|
||
void OnWillRenderCanvases() | ||
{ | ||
if (!running || !Application.isPlaying) | ||
{ | ||
return; | ||
} | ||
|
||
_time += updateMode == AnimatorUpdateMode.UnscaledTime | ||
? Time.unscaledDeltaTime | ||
: Time.deltaTime; | ||
var current = _time / duration; | ||
|
||
if (duration <= _time) | ||
{ | ||
running = loop; | ||
_time = loop ? -loopDelay : 0; | ||
} | ||
_callback(current); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectRunner.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters