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 rmccabe82 · Jan 29, 2015 at 02:57 PM · buttoncolorimagealphaopacity

Cannot set UnityUI.Image opacity when place on a UnityUI.Button

Hi,

I have two buttons, say A and B for arguments sake. Both UnityUI.Buttons contain a UnityUI.Image. For button A, the image colour is set to 1f alpha. For button B, the contained image is set to 0.4f alpha, making it seem dim due to the transparency.

When I click A, I want to reverse the opacity and have A's image with 0.4f alpha, and B's image with 1f alpha.

I have written this code that seems correct:

 public static void SetOpacity(UnityEngine.UI.Image btn, bool enable)
 {
     // Full opacity on the alpha if enabled.
     if (enable)
       btn.color = new Color(btn.color.r, btn.color.g, btn.color.b, 1f);
     else
       btn.color = new Color(btn.color.r, btn.color.g, btn.color.g, 0.4f);
 }

I keep the RGB the same as it originally was for the new colour as I don't need anything but the opacity to change.

I have hooked up click events for both buttons to invoke this code. Which it does as the debug.log is logging! But I cannot see any change in the opacity within the UI. What am I doing wrong? I tried this solution as well, the image doesn't have a "colors" property.

http://answers.unity3d.com/questions/250342/how-to-change-a-color-of-a-gui-button-on-click.html

The attached screen shot shows the button in question, with the image child. You can see the value of the alpha in the image starts at 0.4f for the stop button.

alt text

Thanks in advance!

capture.png (83.4 kB)
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 KurtGokhan · Jan 29, 2015 at 03:13 PM 1
Share

I think it is not working because button does its animations itself. Just disable the animations and see if it works. They can be disabled in the inspector of the Button component. Edit: By animation I mean transition.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Derek-Wong · Jan 29, 2015 at 03:22 PM

noticed that you have put the button's Image in a child rather than at the same object with the button component. is there any reason for this? We cannot judge if this is due to the button's animator (probably not) as we cannot see the button component, please post that here also.

If you really only want to change alpha you may also try to use a canvas group component on your button object (the parent), then you can change all the children's alpha at the same time via script or animation

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 rmccabe82 · Jan 29, 2015 at 04:23 PM

Putting the image as a child of the button meant I could still take advantage of hover states and click of the button when over the top of the image (if the image is at the same level as the button this isn't the case).

It is effected by the transition component of the button, when I set this to None, the opacity code I wrote works, only snag is that I lose the button transitions for disabled, hover etc, which I am using.

alt text


capture.png (93.9 kB)
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 Derek-Wong · Jan 29, 2015 at 04:54 PM 0
Share

cannot get what you exactly means of the advantage of seperate them, but no matter what, I think you can actually set a custom animation in for the transition of the button, and it by setting your custom animation of different state of the button, i believe you should do what you want. you can also reuse the same animator on different buttons.

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

21 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

Related Questions

Button Opacity Animation Not Working 0 Answers

Object opacity/alpha? 1 Answer

Change the button target image alpha 1 Answer

changes to colour alpha affect RGB values, but not in the same way 1 Answer

guiTexture color half alpha White? 1 Answer


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