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 /
avatar image
0
Question by SnailGirl · May 22, 2017 at 03:05 PM · c#alpha-channelextension-methods

alpha method to fade in UI-elements

Hello everyone! I recently started learning C# and I'm currently trying to put together a little project for the first time. All works well so far, except for the change of the Alpha.

I have a panel of images, and depending on if the mouse hovers over the image, leaves it, or clicks it, the Alpha of the image should change. And it does! however, it only changes from the original Alpha 50 to Alpha 255. It does this from a simple hover, even though I've set hovering to only increase to 100. My debugs work fine, unity understands where the mouse is and what it does, but the alpha only change the first time I hover the image :(

In my Extension method I declare that the Alpha should be set with a float, but I get the error that I can't set my double as a float in the image-script. I'm too novice to understand what's up with that.

Does anyone know where I might have slipped up?

Thanks for any replies :)

The extension method to change Alpha

 public static void SetTransparency(this UnityEngine.UI.Image This_image, float p_transparency)
         {
             if (This_image != null)
             {
                 UnityEngine.Color Myalpha = This_image.color;
                 Myalpha.a = p_transparency;
                 This_image.color = Myalpha;
             }


The image script:

 public class Item_Data : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler {
     public Item item;
     public int amount;
     private UiSetup UiSet;
 
 
 
     void Start()
     {
         //find the inventory gameobject, and then the inventory script
         UiSet = GameObject.Find("inventory").GetComponent<UiSetup>();
 
     }
 
     //do this when the pointer clicks an item
     public void OnPointerClick (PointerEventData eventData)
     {
         //if the pointer clicks image, alpha will be full
         if (true)
         {
             GetComponent<Image>().SetTransparency(255);
             Debug.Log("clicked");
 
         }
     }
 
     //when mouse hovers
     public void OnPointerEnter(PointerEventData eventData)
     {
         if (true)
         {
             GetComponent<Image>().SetTransparency(100);
             Debug.Log("hover");
         }
         
     }
 
 
     //when mouse stops hovering
     public void OnPointerExit(PointerEventData eventData)
     {
         if (true)
         {
             GetComponent<Image>().SetTransparency(50);
             Debug.Log("leave");
         }
     }
 
    
 }
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 SnailGirl · May 18, 2017 at 07:33 PM 0
Share

the simple solution was to declare as float if anyone wants to know :) like this: GetComponent().SetTransparency(50f);

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Blurring a generated alpha mask 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Is it possible to RPC a C# extension method? 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