Author Topic: Tween Multiple UISprites  (Read 7697 times)

ivomarel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Tween Multiple UISprites
« on: April 27, 2012, 02:34:19 AM »
Hey,

After buying the full package of NGUI, I found out I basically wasted my time with the free version. It's much better and worth the money! Anyway, I've been trying to use ColorTween to make my UIEquipmentslots fade in. Unfortunately, my UIEquipmenslots consist of three items (Background, Label & Icon). If I tween the whole GameObject, it will only take one of the three items to tween along though. This is first my background, if I remove that it's my Icon, if I remove that, it's my label.

Clearly it only gets one child to tween along.

I'm trying to fix this, but at the same time I thought I'd share it, perhaps someone else knows how to fix it.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tween Multiple UISprites
« Reply #1 on: April 27, 2012, 02:37:41 AM »
That's the problem with tweening color / alpha. It doesn't propagate down to children. It's one of the reasons I suggest using animations instead (scaling / moving). If you really want to fade it like that, you need to get all components of type widget, and tween each separately.

ivomarel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Tween Multiple UISprites
« Reply #2 on: April 27, 2012, 02:47:34 AM »
Ok it works easily like this:

I just changed UIWidget = mWidget into UIWidget[] = mWidgets and on Awake () I added one s in the function mWidgets = GetComponentsInChildren<UIWidget>();

One foreach-loop did the trick
foreach (UIWidget mWidget in mWidgets)
{
   if (mWidget != null) mWidget.color = value;
}

  1.         Transform mTrans;
  2.         UIWidget[] mWidgets;
  3.         Material mMat;
  4.         Light mLight;
  5.  
  6.         /// <summary>
  7.         /// Current color.
  8.         /// </summary>
  9.  
  10.         public Color color
  11.         {
  12.                 get
  13.                 {
  14.                         if (mWidgets[0] != null) return mWidgets[0].color;
  15.                         if (mLight != null) return mLight.color;
  16.                         if (mMat != null) return mMat.color;
  17.                         return Color.black;
  18.                 }
  19.                 set {
  20.                
  21.                         foreach (UIWidget mWidget in mWidgets)
  22.                         {
  23.                                 if (mWidget != null) mWidget.color = value;
  24.                         }
  25.                        
  26.                         if (mMat != null) mMat.color = value;
  27.  
  28.                         if (mLight != null)
  29.                         {
  30.                                 mLight.color = value;
  31.                                 mLight.enabled = (value.r + value.g + value.b) > 0.01f;
  32.                         }
  33.                 }
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Find all needed components.
  38.         /// </summary>
  39.  
  40.         void Awake ()
  41.         {
  42.                 mWidgets = GetComponentsInChildren<UIWidget>();
  43.                 Renderer ren = renderer;
  44.                 if (ren != null) mMat = ren.material;
  45.                 mLight = light;
  46.         }
  47.