I have a project for Android where I need to create a game music mute button. The expected to happen is:
The button on game start (music_on.png): → Highlighted (music_on.png) → Pressed (music_off.png): and it should stay pressed until re-pressed
1- What was happening:
When button pressed it goes to the music_off.png at the moment of the pressing or as long as you keep it pressed and when mouse button released or finger up it goes back to music_on.
highlightedSprite and pressedSprites are loaded in the Inspector with the above used images.
This led to the images changes in the inspector when button is pushed and gives the required button action and image but in the game view or on a physical mobile device I’m still getting the sequence I identified earlier in (1) above.
Hi BoredMormon and thanks for looking into the issue. Actually I thought of the answer you mentioned by in another way by having an UI Image on top of the button and allow the clicks to reach the button and then change the Image.
Still, the script I’ve made above changes the Source Image of the Image component of the button, and I can see it change in the inspector as I run it, but the button image doesn’t change.
My understanding is:
1- The Source Image sprite is the sprite rendered in the normal state for the button
2- If I change the Source Image and move the mouse away of the button (i.e. button should return to normal state) then the renderer should go back to rendering the button’s Source Image not highlighted or pressed…etc
3- My expectation that doesn’t happen is the button to start showing the music_off.png as it is now the Source Image (which never happens)
Is my logic correct or am I missing something here?
The issue is the button is also changing the Image as well. Basically overwriting your changes,
Your script might work as is if you delete the reference to the image on the button component. Or it might just null reference on you , I’m not sure how it’s built internally.
BoredMormon, sorry for debating you further on the issue but here are 2 images that I’ve taken, the first is the Normal Button, the second is after the Button Pressed and mouse moved away from the button, as you can see the image has changed as “Permanently”, please note how the Source Image in the Image component changed the text and the image even in the bottom window, still, the button is still showing the old image (when it was normal)… How is that? Is there a way to force the renderer to refresh for example?
I’d agree with @Kiwasi on this one, The toggle control is a better fit for what you are trying to achieve @waleedhemade . Remember, the controls that unity provide are only examle templates.
I did an example implementation of a toggle like button for Issue #2 of the Unity 3D Magazine, which you can check out here :
A brilliant idea @SimonDarksideJ that you covered in the GUI Evolved article and would really move to that in a future project as actually your article explores great techniques for doing UIs. Still, I’ve solved it the way I described above and to tell you the truth Simon for the sake of this project it made no change and no coding ;). Still, thanks pal and your article changed my concept of thinking in the whole UI. Respect.
Oh and pay attention to the “Write for us” section on the site #NudgeNudge
Got something to offer or want something promoted, then drop a line to the editor!