Author Topic: manual change button color  (Read 3543 times)

mapleegreen

  • Guest
manual change button color
« on: October 01, 2012, 09:54:02 AM »
I want to change button color when needed it.So I add the function in UIButtonColor.cs
  1. public void changeColor (Color color)
  2.         {
  3.                 if (tweenTarget != null)
  4.                 {
  5.                         TweenColor tc = tweenTarget.GetComponent<TweenColor>();
  6.  
  7.                         if (tc != null)
  8.                         {
  9.                                 Debug.Log("we have change the color "+color.ToString());
  10.                                 mColor = color;
  11.                                 tc.color = mColor;
  12.                        
  13.                         }
  14.                 }
  15.         }
but it's failed.Need some tip,thank you.

mtoivo

  • Guest
Re: manual change button color
« Reply #1 on: October 01, 2012, 10:15:09 AM »
How does it fail? With an error? Does your debug statment get printed on console? You're using argument called 'color', but if I remember correctly, 'color' is also also a public variable (well, via getter/setter pair) that UIButton uses internally. Could it overlap? Anyway, I just recenly changed the tint color of spirte by only setting the 'color' variable of the sprite (without a tween, of course). That worked fine.

mapleegreen

  • Guest
Re: manual change button color
« Reply #2 on: October 01, 2012, 11:14:14 AM »
How does it fail? With an error? Does your debug statment get printed on console? You're using argument called 'color', but if I remember correctly, 'color' is also also a public variable (well, via getter/setter pair) that UIButton uses internally. Could it overlap? Anyway, I just recenly changed the tint color of spirte by only setting the 'color' variable of the sprite (without a tween, of course). That worked fine.
I use
  1. UIButton btn = GetComponent<UIButton>();
  2.                         btn.defaultColor = new Color(0.56f,0.42f,0.12f,1.0f);
also has no effect.I didn't find the 'color' variable.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: manual change button color
« Reply #3 on: October 01, 2012, 05:04:52 PM »
That value isn't used until the button's state changes. You need to also TweenColor.Begin on the button's background for it to be immediate.

That said, I really don't recommend doing this. Assuming you're trying to do some sort of a highlight, add a separate sprite covering your button that's normally set to alpha of 0, but when you want it highlighted -- fade it in. This way your code won't conflict with button's internal highlighting logic.