Author Topic: [SOLVED] Good way to execute method after TweenXXX.Begin finishes?  (Read 2042 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
[SOLVED] Good way to execute method after TweenXXX.Begin finishes?
« on: September 13, 2013, 11:38:38 AM »
Hello, I'm using Tween combinations to create animation effects (Like when you click on the popup list) - I want a good way to execute a method after the animation finishes.

I currently I'm doing something like this: (this is a snippet from my custom context menu, which is basically a popup list that appears on the mouse position)

  1. // show on mouse, and seek to animate
  2. if (show && hasFinshedAnimating) {
  3.         _transform.position = camera.ScreenToWorldPoint(Input.mousePosition);
  4.         if (isAnimated) {
  5.                 hasFinshedAnimating = false;
  6.                 for (int i = 0, len = entries.Count; i < len; ++i) {
  7.                         Animate(entries[i]);
  8.                 }
  9.         UIWidget.Animate(background, animSpeed);
  10.         StartCoroutine(WaitForAnim());
  11.         }
  12. }
  13.  
  14. IEnumerator WaitForAnim()
  15. {
  16.         yield return new WaitForSeconds(animSpeed + .15f);
  17.         hasFinshedAnimating = true;
  18.         if (Random.Range(0, 10) > 6)
  19.                 ResetEntriesDims();
  20. }
  21.  
  22. void Animate(Entry entry)
  23. {
  24.         UIWidget.Animate(entry.background, animSpeed);
  25.         UIWidget.Animate(entry.label, animSpeed);
  26. }
  27.  
  28. public static void Animate(UIWidget widget, float animSpeed)
  29. {
  30.         AnimateColor(widget, animSpeed);
  31.         AnimatePosition(widget, animSpeed);
  32.         AnimateScale(widget, animSpeed);
  33. }
  34.  
  35.  

(Notice I'm actually calling Animate methods, but they internally operate on Tween)

It works fine, serves the purpose. But I wonder if there's something built-in for that, something I'm missing perhaps, anything?

In general, can this be done in a better more elegant way?

Thanks!
« Last Edit: September 13, 2013, 12:30:31 PM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Tween.Begin methods return a reference to the tween, letting you do whatever you want with it, including setting the on finished callback.