- Home /
ez gui change texture at runtime
How do I change the texture of an ezgui uiButton from its atlas during runtime. I found out that u need to call the .SetTexture () but im still confused on how to call this and what to do with it.
Thank you
What kind of EZGUI button, and what is the reason to change the state? The reason deter$$anonymous$$es how you approach this problem.
normal ezgui UIbutton.
i have an action button (ezgui UIbutton) for the player to press and attack (touch screen).
currently i have created an inventory of weapons which are made from UIButtons. When the user clicks these buttons i want the action button to change to the selected weapon.
Answer by robertbu · Apr 05, 2013 at 06:44 AM
To implement what you describe, I'd use a State Toggle Button. This type of button allows you to define any number of states, and to set the graphic for each state. For what you describe, you would only need two states. After attaching the State Toggle Button component, select it in the hierarchy and open the UI Control Editor from Unity's Windows menu. You can drag and drop your two states into the editor. The second state will use your weapon graphic. This control will now toggle between the two states when pressed. If necessary, you can prevent the button from returning to its original state with a bit more work.
Will this work if I had many weapons to choose from but only being allowex to equip one at a time?
You can do whatever you want. Think of all the states as frames in an animation. Each state represents one frame. By default the states are walked in order wrapping at the end. But with a bit of code you can define any order you want.
Using State Toggle Buttons or Packed Sprites I often will define a generic game object with a large number of frames/states. Then in code I define the actual state. Think of a universal playing care capable of displaying any card in the deck. When I assign the value of that card in code, the card will display the correct value.
Often I find my answers to EZGUI questions by searching on Above and Beyond's Forum (the maker of EZGUI). You can find the forum here:
As for your question, there are a number of different ways to manage the state of toggle buttons. One way is to subclass the button:
using UnityEngine;
using System.Collections;
public class $$anonymous$$yButton : UIStateToggleBtn {
public override void SetState (int index) {
}
public void $$anonymous$$ySetState(int index) {
base.SetState (index);
}
}
Note this script is derived from UIStateToggleBtn and is attached ins$$anonymous$$d of the State Toggle Button script. It stubs out SetState() and provides a method ($$anonymous$$ySetState()). You now have total control (and total responsibility) for setting the visual state.
A more usual method of managing the state would to add or replace the value change delegate. See the SetValueChangedDelegate() and AddValueChangeDelegate() in the EZGUI documentation.
I'm going to be offline for the next 5 days.
You are to take my code, make it a script, and attach it to the object that needs to have it state managed. Note this script replaces UIStateToggleBtn. After the script is attached, you will use the UIControlEditor to set all the states and images. Since you are changing the state of the button from another game object you will need to create a variable in the script attached to the other game object:
$$anonymous$$yButton mybutton;
Then you will need to initialize this variable either through dragging and dropping or by using GameObject.Find() and GetComponent(). Then when you want to use it:
mybutton.$$anonymous$$ySetState(3);
I'm traveling and will not have access to a computer with Unity for another week.