Author Topic: Change Button Color Tint  (Read 11339 times)

Narv

  • Guest
Change Button Color Tint
« on: August 01, 2013, 01:32:58 PM »
Hey guys.

So I am trying to make a "toggle" button for a gun and I have it as a button, not an image button.  Playing around with the inspector, I see that I like the buttons look for "disabled" when I change the color tint on the background to red, but I see that UISprite does not have a colorTint variable.   I tried changing the color and material.color and neither of them worked.

My desired effect is the button is the way it is, and then when you click on it, it turns red but is still active. then click again and it returns to normal "white" color.

Thanks
« Last Edit: August 06, 2013, 10:51:21 AM by Narv »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Button Color Ting
« Reply #1 on: August 01, 2013, 05:42:02 PM »
UIButton.defaultColor <-- default color of the button
UISprite.color <-- current sprite color

Narv

  • Guest
Re: Change Button Color Tint
« Reply #2 on: August 06, 2013, 11:15:53 AM »
Thanks for the reply. I did get this to work on the PC perfectly, but not on the mobile version. I believe that it is because on the PC version, it "hovers" and changes to green, then when it unhovers it changes back to red. But since mobile doesn't have a hover to green, it doesn't change to red..... but it does but it's offset.


The button is already set and the function is called from a UIButton Message  with onClick calling  toggleMainGun  (this part all works, it calls the function just fine)


  1.         // method that toggles between a red and white button color for the shooting button.
  2.         public void toggleMainGun()
  3.         {
  4.                 // toggle main gun weapon value
  5.                 canUseMainGun = (canUseMainGun == 1) ? 0 : 1;
  6.                
  7.                 mainGunButton.GetComponent<UIButton>().defaultColor = (canUseMainGun == 1) ? Color.white : Color.red;
  8.  
  9.                 // set the playerprefs so the value is saved between game states.
  10.                 PlayerPrefs.SetInt("UseMainGun", canUseMainGun);
  11.         }

When hit the button in the PC version, (editor really, this is a mobile game) I mouse over the button, it turns green (default hover color) and then I click it, mouse out, button is red.. and the gun is disabled as intended.

when I hit the button on the android im testing with, I click press the button down (does the default pressed color) and when I take my finger off.. the color does not change.. but the gun is disabled properly.... if I hit the button again... it turns red... and the gun is enabled... if I hit it again.. it goes white.. and the button is disabled... so the color seems to be offset.

I also tried doing this with UISprite.color like you mentioned.. and I even tried a TweenColor  setting up the from and to and Play() but it doesn't seem to be working either way.  Is there a way I can trigger a hover-like effect of changing to green and then when it changes back it goes to red? and visa versa?

and as for using 1 and 0 instead of true and false is because playerprefs does not have a bool save value (they should... but meh)

Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Button Color Tint
« Reply #3 on: August 07, 2013, 05:37:09 AM »
Instead of doing what you're trying to do, consider setting a disabled color for the button, and actually changing UIButton.isEnabled instead.

Narv

  • Guest
Re: Change Button Color Tint
« Reply #4 on: August 07, 2013, 08:44:22 AM »
if the button is disabled, can they still click the button? I want them to be able to reenable it. I thought they weren't able to click a disabled button, that being the point of disabling the button.

galuodo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 65
    • View Profile
Re: Change Button Color Tint
« Reply #5 on: August 07, 2013, 10:29:42 AM »
u can disable the collider if you do not want users to click them :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change Button Color Tint
« Reply #6 on: August 08, 2013, 10:25:20 AM »
Correct, if a button is disabled, then so is its collider, which means it's not clickable. You can always re-enable it later though (UIButton.isEnabled = true).