Author Topic: One UITween for multiple objects  (Read 2382 times)

fwalker

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
One UITween for multiple objects
« on: September 09, 2013, 09:30:13 AM »
Probably doing something fundamentally wrong here. But....

I would like a parent GameObject to contains a UITween component that can then be run on all its children. Why? Because I don't want to duplicate a UITween on each child and introduce errors if someone has to change the tween behavior.

I added a tween to the parent (it's a Color tween) but only the first child under the parent is changing. The other children (which are identical) are not.

Any thoughts on how I would use one Tween definition on several objects? I know I can use Begin(TargerObject....) but I would like the tween to be defined with in the parent using a UITween component. Is this possible?

 

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: One UITween for multiple objects
« Reply #1 on: September 09, 2013, 10:49:38 AM »
I have not found a "one tween to rule them all" solution like you're proposing. Each GameObject child needs its own tween, since each child has its own material instance that is being colored. I had the same issue when I received some "programmer art" that needed to be colorized: I had to actually traverse the hierarchy and attach tweens to all the children GameObjects.

One advantage to this system, though, is that I can pause my game (and all these color, scale, and position tweens stop right where they are mid-iteration) and resume them afterward with no need to recalculate logic (like "where were they going? how fast? how much time is left to have to create my own lerp?).

  1.     // You can pause the entire object by disabling its tweens, since TweenScale, TweenColor, and
  2.     // TweenPosition all resume perfectly once they are re-enabled.
  3.     //
  4.     // 'canAcceptInput' is a global flag used when identifying NGUI click events: when false, we
  5.     // don't consider this object's collider; when true, we do
  6.     public void PauseObject()
  7.     {
  8.         if (gameObject != null) {
  9.             UITweener[] tweens = gameObject.GetComponents<UITweener>();
  10.             foreach (UITweener tween in tweens) {
  11.                 tween.enabled = false;
  12.             }
  13.         }
  14.         canAcceptInput = false;
  15.     }
  16.  
  17.  
  18.     // Re-enabling the scale, color, and position tweens effectively unpauses the object.
  19.     public void UnpauseObject()
  20.     {
  21.         if (gameObject != null) {
  22.             UITweener[] tweens = gameObject.GetComponents<UITweener>();
  23.             foreach (UITweener tween in tweens) {
  24.                 tween.enabled = true;
  25.             }
  26.         }
  27.         canAcceptInput = true;
  28.     }
  29.  
  30.  
  31.     // Attach a color tween to each child element, as long as it has a renderer attached.
  32.     public void SetObjectColor(Color newColor, float colorTime)
  33.     {
  34.         // Loop through each child transform and, when they have renderers attached to them, tween their colors
  35.         foreach (Transform child in this.transform) {
  36.             if (child.renderer != null) {
  37.                 TweenColor tween = TweenColor.Begin(child.gameObject, colorTime, newColor);
  38.                 tween.onFinished += OnObjectColorFinished;
  39.             }
  40.         }
  41.     }
  42.  
  43.  
  44.     // Called as soon as each color tween has run its course.
  45.     // This callback does not get fired while a tween is paused (disabled) but it WILL get called
  46.     // once the tween is unpaused (re-enabled).
  47.     public OnObjectColorFinished(UITweener tween)
  48.     {
  49.         // Do something once the color tween has completed
  50.     }
  51.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: One UITween for multiple objects
« Reply #2 on: September 10, 2013, 08:53:43 AM »
Why not just make an animation? CTRL+6, and record it. Tweens are for simple stuff. Animations can do anything.