I am trying to change the appearance of a GUI.Button programmatically. I want to be able to do something like this:
GUI.Button myButton = GUI.Button (new Rect... etc.);
And then later in the code:
if (someCondition) {
myButton.skin = customSkin;
else {
myButton.skin = null;
}
Is this possible?
Currently the default GUI in Unity uses the “Immediate Mode” model, which means that it doesn’t remember anything about the controls that are being drawn. You simply change the appearance of buttons by passing different parameters to the GUI methods.
In the case of a button, you can pass variables, in stead of fixed values.
using UnityEngine;
public class DemoClass : MonoBehaviour {
public Rect buttonLocation = new Rect(100,100,250, 40);
public Texture2D buttonTexture;
private int buttonClickCount = 0;
private string buttonText = "ButtonClickCount: ";
private string tooltip = "Appears when you hover the mouse above the button" ;
protected void OnGUI(){
GUILayout.BeginArea(buttonLocation);
GUIContent buttonStyle = new GUIContent(buttonText + buttonClickCount, buttonTexture, tooltip);
if (GUILayout.Button(buttonStyle)){ // in stead of GUILayout.Button("Fixed Value");
buttonClickCount++;
}
GUILayout.EndArea();
}
}
(please note that this example creates a new string every gui update, which might not be very good for memory usage, but it makes for a simple example)
You can also change the appearance of the button by changing the skin that is used. You can see some examples of that in the Unity Documentation. (The way skins are handled seems like a bit of a departure from the ‘immediate mode’ model, because it remembers the skin that is being used.)
There has been some news of a new GUI system in Unity for quite a while now, which would be more like you describe, but that is still some time away.
As RC states, Unity uses the “immediate Mode” model. so you cannot assign a GUI.Button to a variable because it is a function, not a type. how ever there is a way around this. but it may not be recommended.
basically what you can do is make a type of “wrapper” class for that function.you can create the new type that handles the buttons functionality and when to change it elements.
public class GUIButton {
public bool clicked;
public Rect rect;
public GUIcontent contents;
public GUIStyle style;
public GUIButton (Rect rect, GUIcontent contents,GUIStyle style = new GUIStyle) {
this.rect = rect;
this.contents = contents;
this.style = style;
}
public void UpdateGUI() {
clicked = GUI.Button(rect, contents, style);
}
}
with this don’t add it to an object… instead we create a new instance of this class in your GUI script now that we have a type to work with. noticed how i made bool “clicked” and assigned it to the button in UpdateGUI? this is so you can determine if the player has clicked the button or not.
GUIButton mybutton = new GUIButton(new Rect(0,0,120,120), "My Button");
Later in the code
if (someCondition) {
myButton.skin = customSkin;
else {
myButton.skin = null;
}
a must in your OnGUI() block, we want OnGUI to be know we have a button to draw.
void OnGUI() {
myButton.UpdateGUI() //draws the button
}
this code is untested but the methodology works, i actually used this my self to create new element in the GUI system. I don’t get all the fuss about this new GUI?