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
1
Question by yasith1 · Apr 02, 2014 at 08:44 PM · guibuttonimageonclick

How to change button image on click

I need to create a custom button with different images for different states of a button. For an example normal image will be changed in to another image after clicked. I have tried GUISkin but it doesn't allow me to change the position of many buttons. Please suggest a method.

Comment
Add comment · Show 2
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 getyour411 · Apr 02, 2014 at 09:42 PM 0
Share

Changing the position of buttons or change the images based on button state?

avatar image yasith1 · Apr 03, 2014 at 07:26 AM 0
Share

change the images based on button state

2 Replies

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

Answer by Lo0NuhtiK · Apr 03, 2014 at 07:47 AM

     //example 1 variables
     Rect buttonPosition = new Rect(5f, 5f, 50f, 50f) ;
     //drag images onto slots in inspector
     public Texture2D buttonOn, buttonOff ;
     bool buttonIsOn = false ;
 
 
     //example 2 variables
     Rect buttonPositionOne = new Rect(55f, 5f, 50f, 50f) ;
     Rect buttonPositionTwo = new Rect(5f, 55f, 50f, 50f) ;
     bool buttonMoved = false ;
 
     //example 3 variables (uses example 1 images [buttonOn] [buttonOff])
     GUIContent content ;
     Rect posOne = new Rect(105f, 5f, 50f, 50f), posTwo = new Rect(5f, 105f, 50f, 50f) ;
     bool movedButton3 = false ;
 
 
     void OnGUI()
     {
         //EX 1 : a button that changes image when pressed
         if(GUI.Button(buttonPosition, buttonIsOn ? buttonOn : buttonOff))
             buttonIsOn = !buttonIsOn ;
 
         //EX 2 : a button that changes position when pressed
         if(GUI.Button(buttonMoved ? buttonPositionTwo : buttonPositionOne, buttonMoved ? "evoM" : "Move"))
             buttonMoved = !buttonMoved ;
 
         //EX 3 : a button that changes position and content when pressed
         content = movedButton3 ? new GUIContent("xD", buttonOn) : new GUIContent(":)", buttonOff) ;
         if(GUI.Button(movedButton3 ? posTwo : posOne, content))
             movedButton3 = !movedButton3 ;    
     }
Comment
Add comment · Show 1 · 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 yasith1 · Apr 03, 2014 at 04:16 PM 0
Share

Thanks that worked well.

avatar image
0

Answer by Anwar_10 · Apr 03, 2014 at 08:43 AM

Hi.. Using the following code you can change image of button at same position and you want another button position then set coordinate of rectangle and first take two public Texture variable then pass this in GUI.Button.

bool FirstButton=false;

void OnGUI() { if(!FirstButton) { if(GUI.Button(new Rect(340, 720, 90,70),"First Button Image")) { FirstButton= true; } } if(FirstButton) { if(GUI.Button(new Rect(340, 720, 90,70),"Second Button Image")) { FirstButton= false; } } }

Comment
Add comment · 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

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

23 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

Related Questions

Click on box collider on an image in a canvas 0 Answers

Gui.Button Image in Button - error 1502 and 1503 0 Answers

How to access SourceImage component of a button in new GUI 4.6 1 Answer

Buttons become invisible when changing color (on Button or on Image) in script -1 Answers

Putting a delay in a button OnClick ? (Javascript) 3 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