Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed problems with class EffectPlayer, where it would not use delays properly. #245

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Scripts/Common/BaseMeshEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ namespace Coffee.UIEffects
/// </summary>
[RequireComponent(typeof(Graphic))]
[RequireComponent(typeof(RectTransform))]
#if UNITY_2018_3_OR_NEWER
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
{
RectTransform _rectTransform;
Expand Down
135 changes: 87 additions & 48 deletions Scripts/Common/EffectPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using UnityEngine;
//#define LOG_PROGRESS

using UnityEngine;
using System;
using System.Collections.Generic;

namespace Coffee.UIEffects
{
/// <summary>
/// Effect player.
/// </summary>
[Serializable]
public class EffectPlayer
{
//################################
// Public Members.
//################################
#region [Variables]
/// <summary>
/// Gets or sets a value indicating whether is playing.
/// </summary>
Expand Down Expand Up @@ -47,108 +44,150 @@ public class EffectPlayer
/// </summary>
[Tooltip("Update mode")] public AnimatorUpdateMode updateMode = AnimatorUpdateMode.Normal;

// Private variables

static List<Action> s_UpdateActions;

float _delayBeforeContinuing;
float _timePassed;
Action<float> _callback;

#endregion [Variables]

#region OnEnable()
/// <summary>
/// Register player.
/// </summary>
public void OnEnable(Action<float> callback = null)
public void OnEnable( Action<float> callback = null )
{
if (s_UpdateActions == null)
// Register update function on canvas
if( s_UpdateActions == null )
{
#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>REGISTER ON CANVAS</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#} / {_callback} / {play}</color>" );
#endif

s_UpdateActions = new List<Action>();
Canvas.willRenderCanvases += () =>
Canvas.willRenderCanvases += () => // This will happen on each canvas render
{
var count = s_UpdateActions.Count;
for (int i = 0; i < count; i++)
for( int i = 0; i < count; i++ )
{
s_UpdateActions[i].Invoke();
}
};
}

s_UpdateActions.Add(OnWillRenderCanvases);
s_UpdateActions.Add( OnWillRenderCanvases );

if (play)
{
_time = -initialPlayDelay;
}
else
{
_time = 0;
}
_delayBeforeContinuing = initialPlayDelay;
_timePassed = 0;

if( callback != null ) _callback = callback;

_callback = callback;
#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>ENABLED</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#}</color>" );
#endif
}
#endregion OnEnable()

#region OnDisable()
/// <summary>
/// Unregister player.
/// </summary>
public void OnDisable()
{
_callback = null;
s_UpdateActions.Remove(OnWillRenderCanvases);
if( s_UpdateActions != null ) s_UpdateActions.Remove( OnWillRenderCanvases );

#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>DISABLED</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#}</color>" );
#endif
}
#endregion OnDisable()

#region [API] Play()
/// <summary>
/// Start playing.
/// </summary>
public void Play(bool reset, Action<float> callback = null)
public void Play( bool reset, Action<float> callback = null )
{
if (reset)
{
_time = 0;
if( reset ) {
_delayBeforeContinuing = initialPlayDelay;
_timePassed = 0;
}

play = true;
if (callback != null)
{

if( callback != null ) {
_callback = callback;
}

var perc = Mathf.Clamp01( _timePassed / duration );
_callback?.Invoke( perc );

#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>PLAY</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#} / {perc:0.##} / {_callback}</color>" );
#endif
}
#endregion [API] Play()

#region [API] Stop()
/// <summary>
/// Stop playing.
/// </summary>
public void Stop(bool reset)
public void Stop( bool reset )
{
if (reset)
#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>STOP</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#} / {_callback}</color>" );
#endif

if( reset )
{
_time = 0;
if (_callback != null)
{
_callback(_time);
}
_timePassed = 0;
_delayBeforeContinuing = initialPlayDelay;
_callback?.Invoke( 0 );
}

play = false;
}
#endregion [API] Stop()

//################################
// Private Members.
//################################
float _time = 0;
Action<float> _callback;

#region OnWillRenderCanvases() - update each frame
void OnWillRenderCanvases()
{
if (!play || !Application.isPlaying || _callback == null)
{
#if LOG_PROGRESS
Debug.Log( $"<color=cyan><b>UPDATE</b> {_delayBeforeContinuing:0.#} / {_timePassed:0.#} / {_callback} / Playing: {play}</color>" );
#endif

if( !play || !Application.isPlaying || _callback == null ) {
return;
}

_time += updateMode == AnimatorUpdateMode.UnscaledTime

var dTime = updateMode == AnimatorUpdateMode.UnscaledTime
? Time.unscaledDeltaTime
: Time.deltaTime;
var current = _time / duration;

if (duration <= _time)
if( _delayBeforeContinuing > 0 ) {
_delayBeforeContinuing -= dTime;
return;
}

_timePassed += dTime;

var perc = Mathf.Clamp01( _timePassed / duration );
if( perc >= 1 ) // Finished with animation loop
{
play = loop;
_time = loop ? -loopDelay : 0;
play = loop; // Play again, if we're in loop

_timePassed = 0;
_delayBeforeContinuing = loop ? loopDelay : 0;
}

_callback(current);
_callback( perc );
}
}
#endregion OnWillRenderCanvases() - update each frame
}