Author Topic: Changing Texture on Button Press  (Read 3932 times)

vorpalmusic

  • Guest
Changing Texture on Button Press
« on: March 20, 2013, 11:15:27 AM »
Hello.

I'm trying to create a weapon-choosing interface, in which a large button shows and icon for the currently selected weapon, and then when clicked, changes to the next weapon in line from a list of available weapons. So I want the button to show one texture and then show another when clicked.

I have succeeded in getting it to receive a new texture setting, but the button does not update, and still renders the old texture.

I did something like this:

    void OnButton() {
        UISprite button = GameObject.Find("Background").GetComponent<UISprite>();
        button.spriteName = "chestActive";
    }

The texture changes in the Inspector but not in rendering. Any help would be greatly appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing Texture on Button Press
« Reply #1 on: March 20, 2013, 12:05:48 PM »
Not sure what OnButton() is. NGUI has OnClick().

vorpalmusic

  • Guest
Re: Changing Texture on Button Press
« Reply #2 on: March 20, 2013, 12:29:45 PM »
OnButton is the function called by UIButtonMessage when the button is clicked.

I've already described how the texture name changes in the inspector, so the problem is not that the function is not being called.

mikewoz

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Changing Texture on Button Press
« Reply #3 on: June 04, 2013, 12:08:39 PM »
NGUI tries to keep draw calls to a minimum, so you may have to explicitly tell the panel to redraw your sprite. Try this after you change the texture:

  1. button.MarkAsChanged();