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
0
Question by CanisLupus · Oct 21, 2014 at 01:43 PM · animationuibutton

(Unity 4.6) Using Buttons with both Sprite Swap and Animation transition

Is there a way for Buttons in uGUI (Unity 4.6) to have a transition that uses both Sprite Swap and Animation?

We have a generic animator for all buttons in our game, which scales and tints them all in the same way, depending on state (pressed, highlighted, etc). We use the "Animation" transition (and this animator) in all of them.

However, we also want buttons to have a different Sprite for the pressed/disabled/highlighted states, but if we set the transition to "Sprite Swap" we lose the ability to use our animator.

Creating a new animator for each button is out of the question. Is there a native way to do this? We could create a script that detects OnPointerDown/OnPointerUp for the pressed state, but that would leave out the disabled and highlighted states (the latter of which is used both via hovering and keyboard-selecting, I believe).

We are using Unity 4.6.0b21.

Update: I proposed the feature in Unity Feedback, here. For anybody reading this, vote if you're interested. Thanks.

Update 2: I added code for a custom class to do this in my related Unity forum post. It's far from perfect, so I'm waiting for someone else to come up with a better solution. That is the reason I have not accepted any answer yet, or posted my solution as one.

Comment
Add comment · Show 11
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 KayelGee · Oct 21, 2014 at 01:50 PM 0
Share

So if I understand you correctly then you have generic buttons but each button has unique sprites you want to swap to?

avatar image CanisLupus · Oct 21, 2014 at 01:59 PM 0
Share

Every button in the game must scale and tint in the same way on press, hover, etc, which is why we have only one generic animator and use the "Animation" transition. However, despite having the same animation, each button has potentially different sprites in each state (which is exactly what the Sprite Swap transition allows). The buttons themselves are not generic, but their animated transitions are.

avatar image KayelGee · Oct 21, 2014 at 02:12 PM 0
Share

Random thought: Can you add another button component and have one using spriteswap and the other animation? I'm not sure if this is allowed and can't check right now.

avatar image KayelGee · Oct 21, 2014 at 02:26 PM 1
Share

How about writing your own button class? You could derive it from selectable or button. This shouldn't be much work.

avatar image KayelGee · Oct 22, 2014 at 08:45 AM 1
Share

If there is no other simple solution to this you should propose this feature for a future update. Other people might also want this.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by smoggach · Nov 07, 2014 at 04:37 PM

Add another script to your button prefab to change the sprite for you. Then have your generic animation use animationevents to tell this script when to update the sprite. This is why I love the component model -- you can usually get around a problem by adding a small script.

Comment
Add comment · Show 4 · 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 CanisLupus · Nov 11, 2014 at 12:53 PM 0
Share

I confess that I didn't think of using AnimationEvents for this, and it's a nice idea :) The problem with this is that if I add AnimationEvents to my generic animations (to call methods OnHighlighted, OnPressed, etc, on a custom script named ImageSpriteSwapper), I'm REQUIRED to have ImageSpriteSwapper on every button that has the generic animations, or else my console is filled with errors because the events have no receiver. This creates an undesired dependency, as we have buttons where we want the generic animations to play, but no sprite swaps to happen. Currently, I think this can only be achieved with a custom button component, as suggested before, but I might be wrong. Sure, we can add ImageSpriteSwapper to every button and define the sprites as null where needed, but that would be an ugly solution, I think... It should be modular :)

avatar image CanisLupus · Nov 11, 2014 at 12:55 PM 0
Share

By the way, I noticed you gave the answer 3 days ago, but Unity Answers did not notify me in any way (I have the relevant notifications on, though), so I'm sorry for the delay. I noticed the answer almost by accident, and it's not the first time.

avatar image smoggach · Nov 11, 2014 at 01:01 PM 0
Share

I figured that since the button was a prefab then adding a script to the prefab would be no trouble.

avatar image CanisLupus · Nov 11, 2014 at 02:13 PM 1
Share

No, the button is not a prefab. Actually, the only thing they all have in common is their animator. Then they can have sprite swaps if needed (most of them do). We could add a script to every one of them by hand; the problem is just that if no sprite swaps are needed, the script wouldn't be used, which adds unnecessary components. Until we have the time to create and test a custom button component, your answer is still a good (the best?) approach, hence why I gave you a +1.

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

30 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

Related Questions

Unity 4.6 UI Button Click Animation? 4 Answers

Can I make animations snap to a frame? 1 Answer

Activating One Part of An Animation 2 Answers

Prevent animation from starting the first time 0 Answers

Animation works only once on button click 4 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