Graphics.DrawTexture not displaying properly?

Started by
5 comments, last by Dave Hunt 9 years, 2 months ago

Maybe a basic question, but I couldn't find an answer on google.

I'm making a healthbar in Unity C#, and I'm using Graphics.DrawTexture to display the healthbar, but for some reason the texture is blending with the background. The material uses the 'Transparent/Cutout/Diffuse' shader.

Here's the code:


public class Interface : MonoBehaviour 
{
    public Texture2D healthBarTex;
    public Material healthBarMat;
    public float hitPoints;

    private GameObject player;

    void Awake()
    {
        player = GameObject.Find("Player");
    }

    void OnGUI()
    {
        hitPoints = player.GetComponent<Health>().hitPoints;
        if (Event.current.type.Equals(EventType.Repaint))
        {
            Graphics.DrawTexture(new Rect(0, 25, 500, 100), healthBarTex, healthBarMat);
            healthBarMat.SetFloat("_Cutoff", 1 - (hitPoints / 100));
        }
    }
}

And a screenshot:

vzy0sg.jpg

Advertisement


The material uses the 'Transparent/Cutout/Diffuse' shader.

I think you've answered your own question.


The material uses the 'Transparent/Cutout/Diffuse' shader.

I think you've answered your own question.

I don't really see the problem? I'm using a texture with a black and white gradient as an alpha channel:

vzwvab.png

The material looks like this in the inspector:

16a6s6u.jpg

I used this technique once in the past and had no problems with it then.

Also, when I use a plain red texture without alpha channel and as material shader 'Diffuse' I get the same result.

Then it sounds like something you've drawn elsewhere has changed the blend mode or something similar without setting it back.

Then it sounds like something you've drawn elsewhere has changed the blend mode or something similar without setting it back.

Is there a way to check/change the blend mode? Because I have no clue where I could've drawn something that might have changed it.

Sorry, but Unity is not my area of expertise. You might try asking/searching on the Unity Forums.

This topic is closed to new replies.

Advertisement