Unity SetAlpha

Unity Image, SpriteRenderer, TMPro, SetAlpha();

It bothered me to have to create a new Color() just to change the alpha of a spriteRenderer or Image, so i made this simple extesion to set the alpha of anything that inherit the UnityEngine.UI.Graphic class, that includes spriteRenderer, Image, Text, TextMeshPro and so on.

public static class HelpExtensions
{
    public static void SetAlpha<T>(this T p_renderer, float p_alpha) where T : UnityEngine.UI.Graphic
    {
        Color __color = p_renderer.color;

        p_renderer.color = new Color(__color.r, __color.g, __color.b, p_alpha);
    }
}

With this extension you'll be able to just call:

GetComponent<Image>().SetAlpha(1f);
GetComponent<SpriteRenderer>().SetAlpha(1f);
GetComponent<TMPro.TextMeshProUGUI>().SetAlpha(1f);