Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Callback on finished events.

Discussion in 'UGUI & TextMesh Pro' started by yakm, Sep 12, 2014.

  1. yakm

    yakm

    Joined:
    Sep 12, 2014
    Posts:
    24
    In NGUI, after a tweening operation such as tween alpha, color, position was finished. You could delegate a method to be called once it has finished, using the onFinished callback.

    Using uGUI after doing something such as

    newButtonTest.GetComponent<Graphic>().CrossFadeAlpha(0, 1, true);

    This fades an graphic from 1 to 0, is there a way to call a method only once the CrossFadeAlpha has finished?
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,368
    I'd probably use the Animator system for this - set up transition animations with animation events at the end of them.
     
    yakm likes this.
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Last edited: Sep 12, 2014
    yakm likes this.
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. public static class Ease
    6. {
    7.     public enum Type
    8.     {
    9.         Linear,
    10.         Hermite,
    11.         Sinerp,
    12.         Coserp,
    13.         Spring
    14.     }
    15.     private delegate float EaseHandler(float start, float end, float t);
    16.     private static Dictionary<Type, EaseHandler> _types = new Dictionary<Type, EaseHandler>()
    17.     {
    18.         {Type.Linear, Mathf.Lerp},
    19.         {Type.Hermite, Hermite},
    20.         {Type.Sinerp, Sinerp},
    21.         {Type.Coserp, Coserp},
    22.         {Type.Spring, Spring}
    23.     };
    24.     public static void Go(MonoBehaviour o, float start, float end, float t, UnityAction<float> update, UnityAction complete, Type type)
    25.     {
    26.         o.StopAllCoroutines();
    27.         o.StartCoroutine(GoCoroutine(start, end, t, update, complete, type));
    28.     }
    29.     private static IEnumerator GoCoroutine(float start, float end, float t, UnityAction<float> update, UnityAction complete, Type type)
    30.     {
    31.         var i = 0f;
    32.         while (i <= 1f)
    33.         {
    34.             i += Time.deltaTime / t;
    35.             update(_types[type](start, end, i));
    36.             yield return null;
    37.         }
    38.         if (complete != null)
    39.             complete();
    40.     }
    41.     private static float Hermite(float start, float end, float t)
    42.     {
    43.         return Mathf.Lerp(start, end, t * t * (3f - 2f * t));
    44.     }
    45.     private static float Sinerp(float start, float end, float t)
    46.     {
    47.         return Mathf.Lerp(start, end, Mathf.Sin(t * Mathf.PI * .5f));
    48.     }
    49.     private static float Coserp(float start, float end, float t)
    50.     {
    51.         return Mathf.Lerp(start, end, 1f - Mathf.Cos(t * Mathf.PI * .5f));
    52.     }
    53.     private static float Spring(float start, float end, float t)
    54.     {
    55.         t = Mathf.Clamp01(t);
    56.         t = (Mathf.Sin(t * Mathf.PI * (.2f + 2.5f * t * t * t)) * Mathf.Pow(1f - t, 2.2f) + t) * (1f + (1.2f * (1f - t)));
    57.         return start + (end - start) * t;
    58.     }
    59. }
    60.  
     
    yakm likes this.
  5. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
    You could create a copy of the unity's TweenRunner class, extended it and the use it instead of cross fade...

    Check the example project that i've attached.
     

    Attached Files:

    yakm likes this.
unityunity