Author Topic: UIButton multiple tween target on Color Change.  (Read 14585 times)

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
UIButton multiple tween target on Color Change.
« on: June 23, 2013, 09:52:01 PM »
Usually, UIButton is composed of more than one UIWidget, e.g. a UILabel + UISprite. However, the tween target on color change especially "Disabled Color" only affect either one. Is it possible to tween more targets ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #1 on: June 24, 2013, 10:21:45 AM »
Yeah just add more than one UIButton.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #2 on: June 24, 2013, 11:14:41 AM »
UIButtonTween has "Include Children", why don't you implement it for UIButtonColor ?

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #3 on: June 25, 2013, 10:55:22 PM »
I have tried to have two UIButtons that each tween target to Background and Label . But UIButton.isEnabled does not work properly because each UIButton check to the same collider enable state.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #4 on: June 26, 2013, 12:34:12 PM »
And why is that an issue? Disabling the collider disables the button. You should also probably use UIButtonColor instead of a second UIButton if you don't need the disabled state.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #5 on: June 27, 2013, 12:26:42 AM »
I need to define both Background and Label with Disabled Color, So I need two UIButtons, however, either one is not work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #6 on: June 27, 2013, 01:28:32 AM »
Two button scripts on the same collider.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #7 on: June 27, 2013, 01:34:41 AM »
Yes, I did it, but not what you are expected.

  1. public GameObject button;
  2.        
  3.         void OnGUI()
  4.         {
  5.                 if ( GUI.Button( new Rect( 10, 10, 100, 100 ), "Enable" ) )
  6.                 {
  7.                         foreach ( UIButton b in button.GetComponents<UIButton>() )
  8.                         {
  9.                                  b.isEnabled = !b.isEnabled;
  10.                         }
  11.                 }      
  12.                
  13.         }
  14.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #8 on: June 27, 2013, 01:09:23 PM »
First of all, what are you doing mixing OnGUI with NGUI? Second, of course this won't work! "isEnabled" flag for the two buttons is shared! So when you flip it twice, it ends up being exactly what it started with. Instead of a "foreach", do a single GetComponent<UIButton>().isEnabled = !wasEnabled.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #9 on: June 27, 2013, 08:25:05 PM »
Firstly, just ignore the OnGUI stuff.

-Panel
 |-Button (UIButton [Tween Target = Background], UIButton [Tween Target = Label]
    |-Background (UISprite)
    |-Label (UILabel)

Do a single "GetComponent<UIButton>().isEnabled = !wasEnabled" does work for Background but not for Label


yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #10 on: June 28, 2013, 10:12:00 PM »
Firstly, just ignore the OnGUI stuff.

-Panel
 |-Button (UIButton [Tween Target = Background], UIButton [Tween Target = Label]
    |-Background (UISprite)
    |-Label (UILabel)

Do a single "GetComponent<UIButton>().isEnabled = !wasEnabled" does work for Background but not for Label

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #11 on: June 28, 2013, 10:51:54 PM »
Why not? They are both on the same object, same collider. Changing enabled state on one simply enables or disables the collider, which obviously affects the other instantly.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #12 on: June 29, 2013, 06:39:21 AM »
No, that is not the case, would you mind spend some time to try it ? or I send you my project to you?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #13 on: June 29, 2013, 03:41:03 PM »
I'm pretty sure you could have written this script yourself in about a minute.
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void Update ()
  6.         {
  7.                 if (Input.GetKeyDown(KeyCode.G))
  8.                 {
  9.                         UIButton[] buttons = GetComponentsInChildren<UIButton>();
  10.                         bool isEnabled = buttons[0].isEnabled;
  11.                         buttons[0].isEnabled = !isEnabled;
  12.                         for (int i = 1; i < buttons.Length; ++i) buttons[i].UpdateColor(!isEnabled, false);
  13.                 }
  14.         }
  15. }

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIButton multiple tween target on Color Change.
« Reply #14 on: July 01, 2013, 08:31:33 PM »
It works, thanks a lot.