Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by sourav13 · Aug 24, 2016 at 04:02 AM · spritetoggle button

Toggle the Images on Toggle Button .

I want to change the image of a toggle button , different images for on and off button , I can do the image change in code but I think it should be done with UI. alt text The first image shows my toggle button hierarchy and second shows the toggle component off the Music Toggle GameObject , so i have dragged the gameObjects Music On and Music Off to the Graphic and Target Graphic Slot . I can see only one at the time when game is not playing , but in play mode one image toggles (present or not present) but one image is always present. Is it the way it is supposed to be . I don't know how to do it in UI.

screen-shot-2016-08-24-at-92253-am.png (5.1 kB)
screen-shot-2016-08-24-at-92345-am.png (33.5 kB)
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

5 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by TheOtherJoJo · Dec 24, 2016 at 11:51 PM

For the toggle, expand the toggle node. You will see the Background node. Expand the Background node then you will see the Checkmark. Click on Checkmark and set the source image to your On Image.

Next, click on the Background node and set it's source image to your Off Image. Then go back to the Toggle node, set your Transition to Sprite Swap, set the Target Graphic to Background from the scene list, set the Pressed Sprite to your Off Image.

Next, make sure 'Is On' is checked and lastly set Graphic to Checkmark from the scene list.

This is if you want to start your scene with the option on.

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 llMarty · May 06, 2019 at 11:02 PM 0
Share

Thanks, this works. However, the Checkmark is always rendered on top of the pressed sprite on the toggle. How can I see the pressed sprite when clicking to uncheck the toggle, too?

avatar image
1

Answer by JedBeryll · Aug 24, 2016 at 05:26 AM

You can change it within the inspector, set Transition to Sprite swap.

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 sourav13 · Aug 24, 2016 at 06:04 AM 0
Share

@JedBeryll , I understand that setting the Transition to Sprite Swap would give me the options to handle the button sprites on pressed, highlighted and disabled states , but what i want to do is just to change the image to a different one on toggle on/off, that is different image when the is On property is set to true and different when it is set to false.

avatar image JedBeryll sourav13 · Aug 24, 2016 at 06:26 AM 0
Share

Then just set the highlighted and pressed sprites to the On sprite and the disabled to the Off sprite.

avatar image sourav13 · Aug 24, 2016 at 06:50 AM 0
Share

@JedBeryll , What should i put in for target graphic and graphic field . Yes i can see the image on highlighting and pressed , but i don't want to change the image on highlighting and pressing there should be one image for toggle isOn and another for toggle isOff. and should i keep both the music on and music off objects in hierarchy if they are used for target graphic and graphic .

avatar image
1

Answer by Lumpazy · Jun 18, 2018 at 02:06 PM

Toggle did not work for me either in this case ...

What i did : make a button that has a simple uisprite background as its image and delete the text here u can set the background colors and transparencys etc

then make a child of the button : an image. - this image will contain the symbols of your button (sprites in my case) which you wish to switch. In the class where you invoke ( on button pressed ), you include this image as public and assign it in the editor. and also get the two sprites into the class, which you assign the two sprites to. then all you need to do is swap the sprite for the image.

make sure you only raycast the button and not the image with the symbol. hope this helps ... i made an audio play / pause button like this (took me a while, and it was nice to know that the above answer does NOT work :),Toggle did not work for me either in this case ...

What i did : make a button that has a simple uisprite background as its image and delete the text here u can set the background colors and transparencys etc

then make a child of the button : an image. - this image will contain the symbols of your button (sprites in my case) which you wish to switch. In the class where you invoke ( on button pressed ), you include this image as public and assign it in the editor. and also get the two sprites into the class, which you assign the two sprites to. then all you need to do is swap the sprite for the image.

make sure you only raycast the button and not the image with the symbol. hope this helps ... i made an audio play / pause button like this (took me a while, and it was nice to know that the above answer does NOT work :)

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
avatar image
1

Answer by daveinpublic · Jan 11, 2021 at 10:46 PM

I used a Toggle Group and 2 Toggles. The first toggle has my first graphic, my second toggle has my second graphic. The second toggle, I turn off transition, and let the first toggle handle it. I also don't have any function called on my second toggle, because my function on toggle 1 handles all of that.

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
avatar image
-1

Answer by saschandroid · Aug 24, 2016 at 07:44 AM

Set source image of "Background" to "Off" sprite and source image for "Checkmark" to "On" sprite. Now set the RectTransform values of "Background" and "Checkmark" all to zero. An set the anchors of both to min X 0 Y 0 and max X 1 Y 1. They now have both the size of "Toggle". The"Checkmark" sprite will be on top of the "Background" sprite if the toggle is on.

Comment
Add comment · Show 5 · 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 sourav13 · Aug 24, 2016 at 07:57 AM 0
Share

What do u mean by setting the Background image to Off ,(Do u mean to set the source image to None) . second i need the background what's the relation of background to have different images on toggle on/off state . and can you answer what image will be there when the toggle is off , i want different image for off on the top of background image.

avatar image saschandroid sourav13 · Aug 24, 2016 at 08:32 AM 0
Share

Add a new toggle button ... now you see the "Toggle" in the Hierarchy.

"Background" is the first child of "Toggle". The source image for this "Background" will be you "$$anonymous$$usicOff" image. "Checkmark" is the child of "Background". The source image for "Checkmark" will be your "$$anonymous$$usicOn" image. Set all RectTransform values of "Background" and "Checkmark" to zero and the anchors $$anonymous$$ X 0 Y 0 and max X 1 Y 1.

The image shown when the toggle button is off is the sprite you added to "Background". If the toggle button is on, the image of "Checkmark" is shown.

avatar image sourav13 saschandroid · Aug 24, 2016 at 11:45 AM 0
Share

In this way I think if my music off image is larger than music on ,than on the toggle on will it be that the underneath background image will be visible?

Show more comments
avatar image sourav13 · Aug 26, 2016 at 02:21 PM 0
Share

I tried your method , the problem is when i click on isOn toggle button , one image appears on the top of the other , i suppose that the target image and graphic image should just replace each other. Shouldn't they .

The general toggle functionality just toggles the checkmark on and off.

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

74 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 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 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

Character and weapon separate sprites ,Оружие для Персонажа 0 Answers

How to continue material between sprites. 1 Answer

Tiling UI Image sprite horizontally but 9 slice vertically 0 Answers

Sprite Normal Map Outline 1 Answer

Raycast distance in 2D sorting weird with multiple sprites 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