Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jmclaren on April 24, 2013, 08:06:04 AM

Title: UIImageButton Disabled state in 2.6.0
Post by: jmclaren on April 24, 2013, 08:06:04 AM
Settting the disabled state for a UIImageButton  in editor mode changes the pressed state image instead, and the disabled state remains blank. Also, if you disable a button, it uses a random image for the disabled state
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: Nicki on April 24, 2013, 11:09:08 AM
Are the names you put in valid in the same atlas?
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: jmclaren on April 29, 2013, 05:35:56 AM
yes, just picking them from the popup screen
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: Nicki on April 29, 2013, 10:43:53 AM
There was a bug in this script caused by yours truly. I think it may have been fixed already in the newest version of the pro version, and should be pushed out to standard as soon as 1.6.1 gets done.
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on April 29, 2013, 06:02:25 PM
1.6.1, Nicki? :)
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: Nicki on May 02, 2013, 11:10:17 AM
(http://i.imgur.com/vwJC14P.jpg)
Title: Re: UIImageButton Pressed state in 2.6.1
Post by: maoguo.zhang on May 06, 2013, 12:08:52 AM
It seems that UIImageButton.pressedSprite is never used in NGUI, so there is no pressed state when I press the image button.
Title: Re: UIImageButton Pressed state in 2.6.1
Post by: jarjin on May 06, 2013, 09:04:33 PM
It seems that UIImageButton.pressedSprite is never used in NGUI, so there is no pressed state when I press the image button.
+1
Me too~
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on May 06, 2013, 09:06:30 PM
Get 2.6.1b.
Title: Re: UIImageButton Pressed state in 2.6.1c
Post by: maoguo.zhang on May 07, 2013, 04:00:59 AM
Get 2.6.1b.

I got 2.6.1c. But the problem still exists. UIImageButton.pressedSprite which is set in inspector isn't used when pressing image button.
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on May 07, 2013, 05:09:42 AM
How is it not used? Here's the full UIImageButton script from 2.6.1c
  1. public class UIImageButton : MonoBehaviour
  2. {
  3.         public UISprite target;
  4.         public string normalSprite;
  5.         public string hoverSprite;
  6.         public string pressedSprite;
  7.         public string disabledSprite;
  8.        
  9.         public bool isEnabled
  10.         {
  11.                 get
  12.                 {
  13.                         Collider col = collider;
  14.                         return col && col.enabled;
  15.                 }
  16.                 set
  17.                 {
  18.                         Collider col = collider;
  19.                         if (!col) return;
  20.  
  21.                         if (col.enabled != value)
  22.                         {
  23.                                 col.enabled = value;
  24.                                 UpdateImage();
  25.                         }
  26.                 }
  27.         }
  28.  
  29.         void Awake () { if (target == null) target = GetComponentInChildren<UISprite>(); }
  30.         void OnEnable () { UpdateImage(); }
  31.         void OnHover (bool isOver) { if (enabled) UpdateImage(); }
  32.         void OnPress (bool pressed) { if (enabled) UpdateImage(); }
  33.        
  34.         void UpdateImage()
  35.         {
  36.                 if (target != null)
  37.                 {
  38.                         if (isEnabled)
  39.                         {
  40.                                 target.spriteName = UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite;
  41.                         }
  42.                         else
  43.                         {
  44.                                 target.spriteName = disabledSprite;
  45.                         }      
  46.                         target.MakePixelPerfect();
  47.                 }
  48.         }
  49. }
It's clearly used in UpdateImage(), which is called when you set isEnabled.
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: maoguo.zhang on May 07, 2013, 05:20:45 AM
There are hoverSprite, normalSprite and disableSprite in UpdateImage, but pressedSprite is not used.
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on May 07, 2013, 05:24:20 AM
Hmm you're right, I was looking at the disabled sprite.

Looks like Nicki took the liberty of removing something rather important... I'm just going to revert the whole thing and start over.
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on May 07, 2013, 05:29:28 AM
Try this one.
  1. public class UIImageButton : MonoBehaviour
  2. {
  3.         public UISprite target;
  4.         public string normalSprite;
  5.         public string hoverSprite;
  6.         public string pressedSprite;
  7.         public string disabledSprite;
  8.        
  9.         public bool isEnabled
  10.         {
  11.                 get
  12.                 {
  13.                         Collider col = collider;
  14.                         return col && col.enabled;
  15.                 }
  16.                 set
  17.                 {
  18.                         Collider col = collider;
  19.                         if (!col) return;
  20.  
  21.                         if (col.enabled != value)
  22.                         {
  23.                                 col.enabled = value;
  24.                                 UpdateImage();
  25.                         }
  26.                 }
  27.         }
  28.  
  29.         void Awake () { if (target == null) target = GetComponentInChildren<UISprite>(); }
  30.         void OnEnable () { UpdateImage(); }
  31.        
  32.         void UpdateImage()
  33.         {
  34.                 if (target != null)
  35.                 {
  36.                         if (isEnabled)
  37.                         {
  38.                                 target.spriteName = UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite;
  39.                         }
  40.                         else
  41.                         {
  42.                                 target.spriteName = disabledSprite;
  43.                         }      
  44.                         target.MakePixelPerfect();
  45.                 }
  46.         }
  47.  
  48.         void OnHover (bool isOver)
  49.         {
  50.                 if (enabled && target != null)
  51.                 {
  52.                         target.spriteName = isOver ? hoverSprite : normalSprite;
  53.                         target.MakePixelPerfect();
  54.                 }
  55.         }
  56.  
  57.         void OnPress (bool pressed)
  58.         {
  59.                 if (pressed)
  60.                 {
  61.                         target.spriteName = pressedSprite;
  62.                         target.MakePixelPerfect();
  63.                 }
  64.                 else UpdateImage();
  65.         }
  66. }
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: Nicki on May 07, 2013, 07:51:24 AM
Argh, did I fuck it all up? Damnation!
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: maoguo.zhang on May 07, 2013, 08:44:29 PM
Try this one.

I've tested. It's Ok! Thanks!
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: Klegran on May 12, 2013, 07:01:48 PM
To chime in here, there is a bug where after a click, if the button is set to disabled during the click, if the mouse is moved out of the button it will trigger the on hover event and make the button perpetually stuck on "normal" sprite.

The code:
  1. void OnHover (bool isOver)
  2.     {
  3.         if (enabled && target != null)
  4.         {
  5.             target.spriteName = isOver ? hoverSprite : normalSprite;
  6.             target.MakePixelPerfect();
  7.         }
  8.     }

Should be:
  1. void OnHover (bool isOver)
  2.     {
  3.         if (isEnabled && target != null)
  4.         {
  5.             target.spriteName = isOver ? hoverSprite : normalSprite;
  6.             target.MakePixelPerfect();
  7.         }
  8.     }
Title: Re: UIImageButton Disabled state in 2.6.0
Post by: ArenMook on May 13, 2013, 01:22:49 AM
Thanks!