unity-game-enginecolorsspritetexture2dtint

Tint a Texture2D in the same way Unity does


I'm trying to tint a Texture2D with a Color in Unity3D in same same way the sprite renderer does. What is the best way to do this?

Edit: This are the codes i tried:

  private Color Tint(Color source, Color tint) {
    Color tinted = source;
    //tinted.r = Math.Min(Math.Max(0f, (source.r + (255f - source.r) * tint.r)), 255f);
    //tinted.g = Math.Min(Math.Max(0f, (source.g + (255f - source.g) * tint.g)), 255f);
    //tinted.b = Math.Min(Math.Max(0f, (source.b + (255f - source.b) * tint.b)), 255f);

    //tinted.r = (float)Math.Floor(source.r * tint.r / 255f);
    //tinted.g = (float)Math.Floor(source.g * tint.g / 255f);
    //tinted.b = (float)Math.Floor(source.b * tint.b / 255f);

    Color.RGBToHSV(source, out float sourceH, out float sourceS, out float sourceV);
    Color.RGBToHSV(tint, out float tintH, out float tintS, out float tintV);
    tinted = Color.HSVToRGB(tintH, tintS, sourceV);
    tinted.a = source.a;
    return tinted;
  }

Solution

  • Afaik the tint for Renderer in general is done by multiplication (black stays black) and could simply be achieved using the * operator

    private Color Tint(Color source, Color tint) 
    {
        return source * tint;
    }