Fading a texture in and out?

I have a Texture2D (PSD file) that I want to fade in and out as long as the players health is below 10%. I draw the texture using GUI.DrawTexture…

I have no idea how to achieve this, so I need all the help I can get… :slight_smile:

Use GUI.Color before you call GUI.DrawTexture.

GUI.Color.a = 0.5f// Half transparent

If your after a pulsing value you could use a coroutine or iTween.

E.G

when health is below 10% start a coroutine.

Color currentCol;

IEnumerator PulseHealth()
{
  while( Application.isPlaying )
  {
     currentCol.a = Mathf.PingPong( Time.time, 1 );
     yield return 0;
  }
}

// OnGUI

GUI.color = currentCol

  }
}

I found many links to fading GUI, and many links to fading in and out, but a quick search did not yield a combination, so:

#pragma strict

public var speed = 1.0;
public var tex : Texture;

function OnGUI() {
	if (Event.current.type == EventType.Repaint)
		GUI.color.a = Mathf.PingPong(Time.time * speed, 1.0);
		
	GUI.DrawTexture(Rect(0,0,300,300), tex);
}

If you are going to be making other GUI calls that you don’t want to fade, then you can save and restore the alpha value:

#pragma strict

public var speed = 1.0;
public var tex : Texture;
private var alpha = 1.0;


function OnGUI() {
	if (Event.current.type == EventType.Repaint)
		alpha = Mathf.PingPong(Time.time * speed, 1.0);

	GUI.color.a = alpha;
	GUI.DrawTexture(Rect(0,0,300,300), tex);
	GUI.color.a = 1.0;
	
	GUI.Label(Rect(100,100,100,50), "Some Text");
}