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 KnightRiderGuy · Nov 30, 2014 at 05:54 PM · texture2dguitexturegui-buttonbuttonstates

How Do I Make GUI ON Off Button with 2 Button States

OK I have a button with 2 states one one button is not lighted 2nd button state is lighted. So when you click the button it swaps out the GUI button image for the lighted one and when clicked again it swaps it back to the un lighted button. Seems simple enough but I can't find a working sample of how this is done anywhere,

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by b1gry4n · Nov 30, 2014 at 05:58 PM

     if (GUI.Button (Rect (20,40,80,20), "Click")) {
         ButtonToggle();
     }


set up a boolean. on button click call a function.

 private bool isClicked = false;
 
 void ButtonToggle(){
    if(isClicked){
       isClicked = false;
       //swap texture to OFF
    }else{
       isClicked = true;
       //swap texture to ON
    }
 }
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 KnightRiderGuy · Nov 30, 2014 at 06:11 PM 0
Share

Thanks b1gry4n Does this need a var set up for each button texture?

avatar image Kiwasi · Nov 30, 2014 at 06:20 PM 0
Share

Yes, you would typically see a variable for both textures.

avatar image KnightRiderGuy · Nov 30, 2014 at 06:22 PM 0
Share

I'm probably writing it wrong I keep getting errors.

avatar image
0

Answer by YoungDeveloper · Nov 30, 2014 at 06:29 PM

Triggering between states is as easy as using simple bool.

 private bool state = false;
 
 private void Update(){
     if (Input.GetButtonDown("Fire1")){
         state = !state;
 
         if(state) Debug.Log("ON");
         else Debug.Log("OFF");
     }
 }

The rest is up to you how you visually represent it. I'd suggest using 4.6 UI if your not familiar with OnGUI().

Comment
Add comment · Show 7 · 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 KnightRiderGuy · Nov 30, 2014 at 06:55 PM 0
Share

Nope, not getting it are these scripts complete?

avatar image KnightRiderGuy · Nov 30, 2014 at 06:58 PM 0
Share

What I want to do should be old hat I would have thought. All I want to do is make 2 states. One on and one off when clicked it stays one click again button goes off... $$anonymous$$aybe I'm not explaining it right?

avatar image YoungDeveloper · Nov 30, 2014 at 07:24 PM 0
Share

You are explaining it right, you don't have basic understanding for scripting, that's all. Did you wrapped my example in class and added to gameobject?

This will start you going. Trigger between states by clicking right mouse button.

 using UnityEngine;
 
 public class Test : $$anonymous$$onoBehaviour {
     private bool state = false;
 
     private void Update(){
         if (Input.GetButtonDown("Fire1")){
             state = !state;
             if(state) Debug.Log("ON");
             else Debug.Log("OFF");
         }
     }
 
     private void OnGUI(){
         if(state){
             GUI.Box(new Rect(0,0,100,30), "ON"));
         }
     }
 }

avatar image KnightRiderGuy · Nov 30, 2014 at 07:29 PM 0
Share

is this java or C#

avatar image YoungDeveloper · Nov 30, 2014 at 07:31 PM 0
Share

Create c# script called Test, paste the content, add script to gameObject.

Show more comments
avatar image
0

Answer by Cherno · Dec 04, 2014 at 04:33 PM

Sounds like you could just use GUI.Toggle.

 if(GUI.Toggle(yourRect, yourToggleBool, "buttonName") != yourToggleBool) {
             Event.current.Use();
             
             yourToggleBool = !yourToggleBool;
             return;
         }

You can pass it a custom GUI style of your liking that has the proper button on/off images as it's Toggle style.

Comment
Add comment · Show 2 · 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 KnightRiderGuy · Dec 04, 2014 at 04:53 PM 0
Share

Thanks Cherno, How would I make this work with my 2 GUI Texture button states? I would need a var for each texture yes?

avatar image KnightRiderGuy · Dec 04, 2014 at 04:58 PM 0
Share

O$$anonymous$$ This is what I have as java code right now that changes the button on mouse down and back to normal on mouse up. Which is perfect fro my ($$anonymous$$omentary Buttons) I want to make a code similar to this that creates a (Latching button) i.e. Click once for on, click again for off.

 //var levelToLoad : String;
 var mouseUpTexture : Texture2D;
 var mouseDownTexture : Texture2D;
 var beep : AudioClip;
    
 
 function On$$anonymous$$ouseDown(){
      guiTexture.texture = mouseDownTexture;
      audio.PlayOneShot(beep);
 }    
 
 function On$$anonymous$$ouseUp(){
      guiTexture.texture = mouseUpTexture;
      
      yield new WaitForSeconds(0.3);
      //Do Something
 }

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

29 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 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 Button not displaying an icon 3 Answers

ReadPixels requires an offset while in the Editor? 2 Answers

Unassigned Reference Exception. How to create and add a texture 2D in the inspector? 0 Answers

GUI Texture vs Texture2D: which one 1 Answer

Convert Sprite Image to Texture 6 Answers


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