Author Topic: Event Delegates and UItweener  (Read 3255 times)

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Event Delegates and UItweener
« on: March 11, 2015, 09:57:52 AM »
Hi,
I'm having some trouble with a tween controller class where I want to fire of multiple tweens and then wait for them to finish.

So basically I'm starting a tween...building a list with the tweens that are in progress and then removing from the list when they are done...although it doesn't seem to work properly so there must be something I didn't get missed/not understand:

  1.         // Use this for initialization
  2.         void Awake ()
  3.         {
  4.                 _uiTweeners = GetComponents<UITweener> ();
  5.  
  6.                 if (_uiTweeners.Length == 0)
  7.                         _uiTweeners = GetComponentsInChildren<UITweener> ();
  8.  
  9.         }
  10.  
  11.     void Start()
  12.     {
  13.         foreach (UITweener tweener in _uiTweeners)
  14.         {
  15.             EventDelegate onFinishedDelegate= new EventDelegate();
  16.            
  17.             onFinishedDelegate.target=this;
  18.             onFinishedDelegate.methodName="TweenerFinished";
  19.             onFinishedDelegate.parameters[0].obj=tweener;
  20.  
  21.             EventDelegate.Add(tweener.onFinished, onFinishedDelegate);
  22.  
  23.         }
  24.     }
  25.  
  26.    
  27.     void TweenerFinished(UITweener sender)
  28.     {
  29.         tweensInProgress.Remove(sender);
  30.  
  31.         if (tweensInProgress.Count==0 && onFinishedCallback!=null) onFinishedCallback();
  32.     }
  33.  
  34.     public void PlayForward (Action callback)
  35.         {
  36.  
  37.         if (tweensInProgress.Count==0)
  38.         {
  39.  
  40.             onFinishedCallback=callback;
  41.  
  42.                 for (int i = 0; i < _uiTweeners.Length; i++)
  43.             {
  44.                 UITweener tweener=_uiTweeners[i];
  45.  
  46.                 tweensInProgress.Add(tweener);
  47.  
  48.                 tweener.PlayForward();
  49.                 }
  50.         }
  51.    }

Is there anything crazy there?

Stefan

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Delegates and UItweener
« Reply #1 on: March 12, 2015, 07:55:08 PM »
A bit of a strange way of doing it, but your TweenerFinished function needs to be public, not private.

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Event Delegates and UItweener
« Reply #2 on: March 18, 2015, 06:09:16 AM »
Thanks...a totally strange way of doing it...although working now... what would be your advised way of doing it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Delegates and UItweener
« Reply #3 on: March 19, 2015, 06:29:51 AM »
I'd simply assign onFinished only to the tween that has the longest duration.

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Event Delegates and UItweener
« Reply #4 on: March 19, 2015, 10:32:53 AM »
yeah that sounds like efficient way to do it.. so have some code that picks out the longest tween and then assign OnFinished on that one,

well I ended up simplifying my old code with just counting the tweens..I guess not as efficient as your suggestions but more than the first and hopefully less strange:

  1.         foreach (UITweener tweener in _uiTweeners)
  2.         {
  3.             tweener.SetOnFinished(() =>
  4.             {
  5.                 tweenersInProgressCount--;
  6.                 if (tweenersInProgressCount == 0 && onFinishedCallback != null)
  7.                     onFinishedCallback();
  8.             });
  9.         }