- Home /
GUI - Permanent Selection
I would like to have my active GUIStyle stay visible while it is actually active, in the sense of like a Sound Mute button. What is the best way to do so? I've been stuck with it for a little while.
Willing to elaborate if needed.
Thanks in advance!
Answer by pako · Oct 26, 2013 at 09:51 AM
The GUIStyle is a collection of custom attributes that defines the appearance of a single UnityGUI Control. So the GUIStyle itself is never visible. If you want to create a GUI button then use GUI.Button. You'll get 2 screen buttons if you attach the following example script on an Empty GameObject:
http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html
And what exactly do you mean permanent? For instance, in the above example, the 2 buttons will be permanent in the scene as long as the GameObject that the screen is attached to is enabled (active). However, if you load a different scene and you want to make the button permanent also between scenes, you have to use DontDestroyOnLoad. So add the code in the following example, in the Awake() method of the script containing the GUI.Button code:
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
If this helps you please accept the answer (click the check mark icon). Otherwise, please clarify your question and elaborate.
UPDATE: The following script provides the functionality you need:
using UnityEngine;
using System.Collections;
//Create an empty GameObject in the hierarchy and attach this script
public class MuteButton : MonoBehaviour {
public Texture ButtonSoundOn; //Displayed when the sound is on. When clicked will mute the sound
public Texture ButtonSoundOff; //Displayed when the sound is off. When clicked will turn the sound on
private bool soundOn;
//If you need the Mute button to shown on all scenes, use the following code
void Awake()
{
DontDestroyOnLoad(gameObject);
}
// Use this for initialization
void Start()
{
soundOn = true; //if the sound is on when the scene starts set this flag to true
}
void OnGUI()
{
if (soundOn) //Do this when the sound is ON
{
if (!ButtonSoundOn)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 50, 50), ButtonSoundOn)) //display button with ButtonSoundOn Texture
{
//if the ON button is clicked, mute the sound
//and set the flag to indicate the sound is OFF
//code to mute sound
soundOn = false;
}
}
else //Do this when the sound is OFF
{
if (!ButtonSoundOn)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 50, 50), ButtonSoundOff)) //display button with ButtonSoundOff Texture
{
//if the OFF button is clicked, turn the sound ON
//and set the flag to indicate the sound is ON
//code to turn the sound on
soundOn = true;
}
}
}
}
Sorry. I'll elaborate, What I want is a sound button, for muting and un-muting purposes. I want it so when the person selects the 'Off' button, it stays as the 'Depressed' texture. If you get what I mean. Then if the person wants it on, they hit the 'On' button, then that would stay in the 'Depressed' texture.
Sorry if my elaboration is vague. I'm quite tired.
@Nivalis_Brayden the script I posted provides the functionality you asked for, but you have not accepted it as an answer. Could you please clarify why?
Sorry, Haven't had time to look, It works, thank-you.
Your answer

Follow this Question
Related Questions
GUI help || Information 1 Answer
Timer With GUI 0 Answers
GUI follow gameobject 2 Answers
gui elements to clo 1 Answer
Button hover texture on state 1 Answer