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
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 6 · 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 2
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.

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

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

CrossPlatformInput Detect Multiple Input Headache 1 Answer

UI Button OnClick() Will not Trigger Function Call 0 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