Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nivalis_Brayden · Oct 26, 2013 at 06:26 AM · gui stylegui state

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
             }
         }
 
     }
 }
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nivalis_Brayden · Oct 26, 2013 at 09:58 AM 0
Share

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.

avatar image pako · Oct 27, 2013 at 10:05 AM 0
Share

@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?

avatar image Nivalis_Brayden · Oct 27, 2013 at 11:17 AM 0
Share

Sorry, Haven't had time to look, It works, thank-you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges