Author Topic: Button backgound image changes on Hover  (Read 2632 times)

Abe_ForkTech

  • Guest
Button backgound image changes on Hover
« on: February 12, 2013, 02:16:34 AM »
Hi,

First of all I am sorry if this question has been answered before, as I could not find anything on this so posting it in a new thread.

So, I have a couple of buttons, I was able to change the background image of the button when pressed (to pressed image). But the problem is when I press a button its image is changed to pressed image I have and at the same time I am changing the images of the other buttons back to the unpressed image. Now the other buttons images are not updated and they keep on showing the same image (pressed image) until or unless I hover on them.

Is there a way to force the button to change its image instantly?

This is how I am changing the images of the buttons by getting their sprites in an array.

for (int i=1; i<=2; i++)
{
   if (i == data.id) {             //data.id the number of the button pressed
      TabButtonsSprites[i-1].sprite = TabButtonsSprites[i-1].atlas.GetSprite("sticker_pressed");
      }
      else
      {
         TabButtonsSprites[i-1].sprite = TabButtonsSprites[i-1].atlas.GetSprite("sticker");
            
      }
         
}

Regards,
Abe

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button backgound image changes on Hover
« Reply #1 on: February 12, 2013, 09:59:45 AM »
I assume you're using UIImageButton since you didn't mention any specifics. You need to change the actual background sprite, not just the UIImageButton's sprite names.

Also... never use .sprite = .... Set spriteName field instead.

Abe_ForkTech

  • Guest
Re: Button backgound image changes on Hover
« Reply #2 on: February 18, 2013, 03:32:55 AM »
Hi Aren,

Thanks for the reply, and sorry I didn't post earlier. I was not using UIImageButton, I was using UIButton. But I was able to fix the issue. Read the same fix (spriteName) you posted in some other thread. Thanks again.

Rite now I am having another issue if you are kind enough to help me with it. I am trying to set a texture using UITexture and I have read I can directly set the UITexture.mainTexture property. Put when I do this it shows an error saying UIWidget.mainTexture is read only.  :(

Here is what I have done:

public UITexture crosshair = null;
public Texture  crosshairTexture = null;
   
// Use this for initialization
void Start () {
   crosshair.mainTexture = crosshairTexture;
}

Please let me know what I am doing wrong or if I am missing something..??

Regards,
Abe

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button backgound image changes on Hover
« Reply #3 on: February 18, 2013, 06:01:08 AM »
Make sure you are using the latest version. It should work as-is. UITexture.mainTexture has a setter.

Abe_ForkTech

  • Guest
Re: Button backgound image changes on Hover
« Reply #4 on: February 18, 2013, 06:05:05 AM »
yup thought about it but couldn't find the link to download the latest  :-[