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 SUBZERO8K · Oct 24, 2014 at 06:40 PM · uibuttonbetahighlighting

Unity 4.6 Change Highlighted Button Sprite using C#

Hi there,

I am currently using the SpriteSwap transition mode for a button in the Unity 4.6 beta, and was wondering if there is there a way to permanently change the highlighted sprite from the original assigned in the Inspector through code when that button is pressed?

Comment
Add comment · Show 1
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 kolibro89 · Mar 26, 2015 at 10:54 PM 0
Share

Did you solve it? I have the same problem and can't find any way to set the highlighted sprite.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by kolibro89 · Mar 27, 2015 at 04:55 PM

I'm so happy, I've found an answer that worked for me here:

 Sprite newSprite = Resources.Load<Sprite>("sprites/somesprite");
 SpriteState st = new SpriteState();
 st.disabledSprite = newSprite;
 st.highlightedSprite = newSprite;
 st.pressedSprite = newSprite;
 _button.spriteState = st; 

I think that means that you cannot access the variables in the existing SpriteState, but you can just create a new SpiteState that overrides the old one :-D

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
0

Answer by Anxo · Oct 24, 2014 at 07:12 PM

It looks a little tricky at the moment, Its very fresh meat and needs to get a good cooking. But I think its the image variable in the selected stuff.

http://docs.unity3d.com/460/Documentation/ScriptReference/UI.Selectable.html

I have not been able to test it as I have not been able to access the UI namespace yet

But I think this answer might get you a little closer to a solution http://answers.unity3d.com/questions/780629/unity-46-gui-select-button-via-script.html

a little convoluted right now and I am sure it will change.

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 Umai · Dec 29, 2014 at 06:17 AM 0
Share

So, has this changed? I still can't find a solution how to get/set the sprite that is used for different states like pressed etc.

avatar image
0

Answer by Corefinder · Mar 25, 2015 at 11:37 PM

Here is something I found on another post: using UnityEngine; using UnityEngine.UI; using System.Collections;

  public class TestMenu : MonoBehaviour 
  {
      /// <summary>
      /// This is the Sprite we're going to swap to.
      /// </summary>
      /// <remarks>
      /// This is set in Unity's Inspector.
      /// </remarks>
      public Sprite OtherSprite;
  
      /// <summary>
      /// The Array of Images.
      /// </summary>
      Image[] images;
  
      // Use this for initialization
      void Start() 
      {
          // Get all components of type Image that are children of this GameObject.
          images = gameObject.GetComponentsInChildren<Image>();
  
          // Loop through each image and set it's Sprite to the other Sprite.
          foreach (Image image in images)
          {
              image.sprite = OtherSprite;
          }
      }
  }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Buttons are fully functional but have never highlighted or pressed down in my 1+ year unity journey. 1 Answer

[4.6 UI] Possible to reference parameter of OnClick persistent event? 0 Answers

Unity 4.6 Beta Controller Menu Button Movement 0 Answers

I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers

How to make UI buttons so my player can move along certain lines 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