Skip to content

Commit

Permalink
close #87; Use Canvas.willRenderCanvases event instead of Update method
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai authored and mob-sakai committed Sep 27, 2018
1 parent ea7d98d commit eb0bea2
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 96 deletions.
117 changes: 117 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectRunner.cs
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 26 additions & 29 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ public class UIDissolve : UIEffectBase
[SerializeField] Texture m_NoiseTexture;
[SerializeField] protected EffectArea m_EffectArea;
[SerializeField] bool m_KeepAspectRatio;

[Header("Effect Runner")]
[SerializeField] EffectRunner m_Runner;

[Header("Play Effect")]
[Obsolete][HideInInspector]
[SerializeField] bool m_Play = false;
[Obsolete][HideInInspector]
[SerializeField][Range(0.1f, 10)] float m_Duration = 1;
[Obsolete][HideInInspector]
[SerializeField] AnimatorUpdateMode m_UpdateMode = AnimatorUpdateMode.Normal;


Expand Down Expand Up @@ -161,19 +168,29 @@ public bool keepAspectRatio
public ColorMode colorMode { get { return m_ColorMode; } }

/// <summary>
/// Play dissolve on enable.
/// Play effect on enable.
/// </summary>
public bool play { get { return m_Runner.running; } set { m_Runner.running = value; } }

/// <summary>
/// Play effect loop.
/// </summary>
public bool play { get { return m_Play; } set { m_Play = value; } }
public bool loop { get { return m_Runner.loop; } set { m_Runner.loop = value; } }

/// <summary>
/// Dissolve duration.
/// The duration for playing effect.
/// </summary>
public float duration { get { return m_Duration; } set { m_Duration = Mathf.Max(value, 0.1f); } }
public float duration { get { return m_Runner.duration; } set { m_Runner.duration = Mathf.Max(value, 0.1f); } }

/// <summary>
/// Dissolve update mode.
/// Delay on loop effect.
/// </summary>
public AnimatorUpdateMode updateMode { get { return m_UpdateMode; } set { m_UpdateMode = value; } }
public float loopDelay { get { return m_Runner.loopDelay; } set { m_Runner.loopDelay = Mathf.Max(value, 0); } }

/// <summary>
/// Update mode for playing effect.
/// </summary>
public AnimatorUpdateMode updateMode { get { return m_Runner.updateMode; } set { m_Runner.updateMode = value; } }

/// <summary>
/// Modifies the material.
Expand Down Expand Up @@ -256,8 +273,7 @@ public override void ModifyMesh(VertexHelper vh)
/// </summary>
public void Play()
{
_time = 0;
m_Play = true;
m_Runner.Play();
}


Expand All @@ -269,15 +285,16 @@ public void Play()
/// </summary>
protected override void OnEnable()
{
_time = 0;
base.OnEnable();
m_Runner.OnEnable(f => location = f);
}

protected override void OnDisable()
{
MaterialCache.Unregister(_materialCache);
_materialCache = null;
base.OnDisable();
m_Runner.OnDisable();
}

#if UNITY_EDITOR
Expand All @@ -295,25 +312,5 @@ protected override Material GetMaterial()
// Private Members.
//################################
MaterialCache _materialCache = null;
float _time = 0;

void Update()
{
if (!m_Play || !Application.isPlaying)
{
return;
}

_time += m_UpdateMode == AnimatorUpdateMode.UnscaledTime
? Time.unscaledDeltaTime
: Time.deltaTime;
location = _time / m_Duration;

if (m_Duration <= _time)
{
m_Play = false;
_time = 0;
}
}
}
}
91 changes: 24 additions & 67 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ public class UIShiny : UIEffectBase
[SerializeField][Range(0, 1)] float m_Brightness = 1f;
[SerializeField][Range(0, 1)] float m_Highlight = 1;
[SerializeField] protected EffectArea m_EffectArea;
[Header("Play Effect")]
[Header("Effect Runner")]
[SerializeField] EffectRunner m_Runner;

[Obsolete][HideInInspector]
[SerializeField] bool m_Play = false;
[Obsolete][HideInInspector]
[SerializeField] bool m_Loop = false;
[Obsolete][HideInInspector]
[SerializeField][Range(0.1f, 10)] float m_Duration = 1;
[Obsolete][HideInInspector]
[SerializeField][Range(0, 10)] float m_LoopDelay = 1;
[Obsolete][HideInInspector]
[SerializeField] AnimatorUpdateMode m_UpdateMode = AnimatorUpdateMode.Normal;


Expand Down Expand Up @@ -191,46 +198,48 @@ public EffectArea effectArea
/// <summary>
/// Play shinning on enable.
/// </summary>
public bool play { get { return m_Play; } set { m_Play = value; } }
public bool play { get { return m_Runner.running; } set { m_Runner.running = value; } }

/// <summary>
/// Play shinning loop.
/// </summary>
public bool loop { get { return m_Loop; } set { m_Loop = value; } }
public bool loop { get { return m_Runner.loop; } set { m_Runner.loop = value; } }

/// <summary>
/// Shinning duration.
/// </summary>
public float duration { get { return m_Duration; } set { m_Duration = Mathf.Max(value, 0.1f); } }
public float duration { get { return m_Runner.duration; } set { m_Runner.duration = Mathf.Max(value, 0.1f); } }

/// <summary>
/// Delay on loop.
/// </summary>
public float loopDelay { get { return m_LoopDelay; } set { m_LoopDelay = Mathf.Max(value, 0); } }
public float loopDelay { get { return m_Runner.loopDelay; } set { m_Runner.loopDelay = Mathf.Max(value, 0); } }

/// <summary>
/// Shinning update mode.
/// </summary>
public AnimatorUpdateMode updateMode { get { return m_UpdateMode; } set { m_UpdateMode = value; } }
public AnimatorUpdateMode updateMode { get { return m_Runner.updateMode; } set { m_Runner.updateMode = value; } }

/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable()
{
_time = 0;
// graphic.material = effectMaterial;
base.OnEnable();
// m_Runner.Register(m_Play, m_Duration, m_UpdateMode, m_Loop, m_LoopDelay, f => location = f);
m_Runner.OnEnable(f => location = f);
}

/// <summary>
/// This function is called when the behaviour becomes disabled () or inactive.
/// </summary>
protected override void OnDisable()
{
base.OnDisable();
m_Runner.OnDisable();
}

// /// <summary>
// /// This function is called when the behaviour becomes disabled () or inactive.
// /// </summary>
// protected override void OnDisable()
// {
// graphic.material = null;
// base.OnDisable();
// }

#if UNITY_EDITOR
Expand All @@ -239,28 +248,6 @@ protected override Material GetMaterial()
return MaterialResolver.GetOrGenerateMaterialVariant(Shader.Find(shaderName));
}

// public void OnBeforeSerialize()
// {
// }
//
// public void OnAfterDeserialize()
// {
// var obj = this;
// EditorApplication.delayCall += () =>
// {
// if (Application.isPlaying || !obj)
// return;
//
// var mat = GetMaterial(shaderName);
// if (m_EffectMaterial == mat && graphic.material == mat)
// return;
//
// graphic.material = m_EffectMaterial = mat;
// EditorUtility.SetDirty(this);
// EditorUtility.SetDirty(graphic);
// EditorApplication.delayCall += AssetDatabase.SaveAssets;
// };
// }
//
// public static Material GetMaterial(string shaderName)
// {
Expand Down Expand Up @@ -328,42 +315,12 @@ public override void ModifyMesh(VertexHelper vh)
/// </summary>
public void Play()
{
_time = 0;
m_Play = true;
m_Runner.Play();
}


//################################
// Private Members.
//################################
float _time = 0;

void Update()
{
if (!m_Play || !Application.isPlaying)
{
return;
}

_time += m_UpdateMode == AnimatorUpdateMode.UnscaledTime
? Time.unscaledDeltaTime
: Time.deltaTime;
location = _time / m_Duration;

if (m_Duration <= _time)
{
m_Play = m_Loop;
_time = m_Loop ? -m_LoopDelay : 0;
}
}

// /// <summary>
// /// Mark the UIEffect as dirty.
// /// </summary>
// void _SetDirty()
// {
// if (graphic)
// graphic.SetVerticesDirty();
// }
}
}

0 comments on commit eb0bea2

Please sign in to comment.