- Home /
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");
}
}
}
the simple solution was to declare as float if anyone wants to know :) like this: GetComponent().SetTransparency(50f);
Your answer
Follow this Question
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