• 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
1
Question by guitarninja · Jun 07, 2016 at 05:42 PM · uibuttonsprite

How do you change pressed sprite (C#)

On a unity UI Button there is an option called "Pressed Sprite" and this is the sprite which is displayed when you are clicking on the button. I'd like to be able to change the "Pressed Sprite" sprite in code.

Here's an example of what I've tried

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 void Awake () 
 {
     button = this.gameObject.GetComponent<Button>();
 }

 public void SetSprite()
 {
     if (enabled) {
         button.spriteState.pressedSprite = redSprite;
     } else {
         button.spriteState.pressedSprite = greenSprite;
     }

I also tried setting up a temp variable such as button = tempButton; and that was the same

I tend to get the following errors:

 error CS0029: Cannot implicitly convert type `UnityEngine.Sprite' to `UnityEngine.UI.SpriteState'

 error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.spriteState'. Consider storing the value in a temporary variable
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
7
Best Answer

Answer by TBruce · Jun 07, 2016 at 06:30 PM

Coding is not necessary. Do this

  1. Change the button Transition to Sprite Swap

  2. Set the Pressed Sprite to the desired sprite

  3. You can also set Highlighted Sprite and Disabled Sprite

  4. Your good to go

Is this what you are trying to achieve?

alt text


inspector.png (20.4 kB)
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 guitarninja · Jun 07, 2016 at 06:52 PM 0
Share

Above works however I need the Pressed Sprite to actually change to a different Pressed Sprite during run-time.

avatar image TBruce guitarninja · Jun 07, 2016 at 07:00 PM 0
Share

What do you mean a different Pressed Sprite? The Sprite Image and the Pressed Sprite must be different for this to work so when you press the button the Sprite Image is swapped out with the Pressed Sprite and the button will show the Pressed Sprite as long as the button is held down. Then when the button is released the original Sprite Image is swapped back in.

avatar image guitarninja TBruce · Jun 07, 2016 at 07:14 PM 0
Share

I'd like to be able to swap the sprite during run-time. Just like you might want to change the "Source Image" sprite on the "Image" component

Show more comments
avatar image guitarninja · Jun 07, 2016 at 08:09 PM 1
Share

PERFECT! I've seen this question a few times but with no actual answers.

I think I kept tripping over the syntax for this but the below part has allowed me to get it working

              SpriteState spriteState = new SpriteState();
              spriteState = button.spriteState;
              if (enabled)
              {
                  spriteState.pressedSprite = redSprite;
              }
              else
              {
                  spriteState.pressedSprite = greenSprite;
              }
              button.spriteState = spriteState;
avatar image TBruce guitarninja · Jun 07, 2016 at 08:45 PM 1
Share

Thank you. I am glad I could help. I hope that your project is a success.

avatar image $$anonymous$$ · Jul 29, 2016 at 09:00 AM 0
Share

Such a wonderful answer

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

How to change a button image with script? 1 Answer

UI button class 1 Answer

Smooth UI sprite animation. 1 Answer

Unity UI Problem 0 Answers

[SOLVED] GUI Button Rotate animation Unity 5 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges