Author Topic: UIButtonColor Issue: Setting color before intial state change  (Read 2114 times)

Hatschupp

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Hi,
I'm was trying to change the state colors of a button with several UIButtonColor components via code, before it received any input event. Unfortunately this didn't work, unless I hovered over (or clicked) the button at least once before the change.

I fixed this by adding this to UIButtonColor:

  1.  
  2.     /// <summary>
  3.     /// Init the visual state.
  4.     /// </summary>
  5.  
  6.     protected virtual void InitState() {
  7.         TweenColor.Begin(tweenTarget, duration, mColor);
  8.     }
  9.  

And I called the method in OnInit().

Maybe I was missing something, otherwise it would be nice if you could add something like this to your script. (Don't like messing with your code, it such a pain to merge changes everytime an update arrives  :) )

Cheers

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtonColor Issue: Setting color before intial state change
« Reply #1 on: April 29, 2014, 02:20:22 PM »
UIButtonColor creates a tween to change the color. If you do your own tween, it will conflict with this behaviour.

Hatschupp

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIButtonColor Issue: Setting color before intial state change
« Reply #2 on: April 30, 2014, 05:59:19 AM »
I don't use an extra tween, I'm changing the button's default and disabled color variables via code to reflect certain game-internal states. I'm still using the auto-created tween for hover etc.

But unless this tween has been created, I can't change the colors, although the tween apparently has nothing to do with the color changes in script.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtonColor Issue: Setting color before intial state change
« Reply #3 on: April 30, 2014, 05:29:08 PM »
Tween only changes the sprite's current color. The button script creates a tween to transition from color A to color B. What colors "A" and "B" are depends on the state it's transitioning from. For example from normal to hovered, or hovered to pressed.

If you want to change the default color of the button, use UIButtonColor.defaultColor. The active state won't be updated if you change it on the fly. You would need to check the button's current state, and if it is what you expect -- do your own TweenColor.Begin exactly the same way it happens in UIButtonColor.SetState.

Hatschupp

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIButtonColor Issue: Setting color before intial state change
« Reply #4 on: May 04, 2014, 04:53:51 AM »
I do use the .defaultColor /.disabled etc. variables to change the button's colors, and as you said the active state is not getting updated on the fly. Unless I use the method mentioned above once. Afterwards I can change the colors on the fly without problems. Not exactly sure why it works though  ???