Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: yuewah on June 23, 2013, 09:52:01 PM

Title: UIButton multiple tween target on Color Change.
Post by: yuewah 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 ?
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on June 24, 2013, 10:21:45 AM
Yeah just add more than one UIButton.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah on June 24, 2013, 11:14:41 AM
UIButtonTween has "Include Children", why don't you implement it for UIButtonColor ?
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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.
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook 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.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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.
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on June 27, 2013, 01:28:32 AM
Two button scripts on the same collider.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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.  
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook 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.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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

Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook 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.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah 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?
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook 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. }
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah on July 01, 2013, 08:31:33 PM
It works, thanks a lot.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah on August 20, 2013, 01:00:50 AM
I would suggest the following fix for updating other UIButton script color

  1. [AddComponentMenu("NGUI/Interaction/Button")]
  2. public class UIButton : UIButtonColor
  3. {
  4.  
  5.         public bool isEnabled
  6.         {
  7.                 get
  8.                 {
  9.                         Collider col = collider;
  10.                         return col && col.enabled;
  11.                 }
  12.                 set
  13.                 {
  14.                         Collider col = collider;
  15.                         if (!col) return;
  16.                        
  17.                         if (col.enabled != value)
  18.                         {
  19.                                 col.enabled = value;
  20.                                
  21.                                 UIButton[] buttons = this.gameObject.GetComponentsInChildren<UIButton>();
  22.                                 for (int i = 0; i < buttons.Length; ++i)
  23.                         buttons[i].UpdateColor(value, false);
  24.                         }
  25.                 }
  26.         }
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah on August 20, 2013, 08:59:47 PM
ArenMook, could you add this to the release
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on August 21, 2013, 10:20:35 AM
No I can't. What if you have more than one button script in the hierarchy, and you don't want this behaviour? Forcing it is a bad idea.
Title: Re: UIButton multiple tween target on Color Change.
Post by: yuewah on August 21, 2013, 12:57:07 PM
sorry, it should be in the same level
  1.  
  2.                 UIButton[] buttons = this.gameObject.GetComponents<UIButton>();
  3.                 for (int i = 0; i < buttons.Length; ++i)
  4.                         buttons[i].UpdateColor(value, false);
  5.  

Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on August 22, 2013, 11:33:52 AM
That's more reasonable, but honestly if you need this kind of functionality you should just create a custom UIButton class for yourself.
Title: Re: UIButton multiple tween target on Color Change.
Post by: imperso on June 11, 2015, 06:25:49 AM
Could somebody provide a working setup of such button?
Currently my button contains two overlapping sprites:

- UIButton with Collider
-- UISprite
-- UISprite

TweenTarget was set to the same GameObject with UIButton but is always automatically reset to None.
TweenColor doesn't work at all.

How should I rework my setup to make the button change color of each sprite it consists of?
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on June 11, 2015, 06:46:11 AM
UIButton's tween target should point to one of the sprites, not itself.
Title: Re: UIButton multiple tween target on Color Change.
Post by: imperso on June 11, 2015, 08:37:08 AM
Yes, but my button consists of two overlapping sprites, and I need the button to change hover color of both of them, not just one. Is there any layout to this without creating custom UIButton class?
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on June 11, 2015, 09:18:09 AM
Just attach two UIButton scripts. One pointing to the first sprite, the other button pointing to the second sprite.
Title: Re: UIButton multiple tween target on Color Change.
Post by: imperso on June 11, 2015, 10:43:31 AM
Thanks, I figured it out.
The problem is - my sprites overlap. So if I click the area of overlapping, my button technically fires twice.

EDIT: Overlapping is not the case - it seems that such button always fires twice.

EDIT2: Found some solution by adding UIButtonColor and pointing it to the second sprite (first one is already connected to the main UIButton).
Alas, it seems this component is deprecated (NGUI suggests me "to upgrade to a Button").
Title: Re: UIButton multiple tween target on Color Change.
Post by: ArenMook on June 12, 2015, 08:03:25 AM
Button fires twice? Only one button script should have the event delegates set. Second should be used only for color changes.
Title: Re: UIButton multiple tween target on Color Change.
Post by: imperso on June 12, 2015, 01:08:11 PM
Yep, eventually this was my mistake.