Fade between textures in the same script?

Another issue I can't seem to wrap my head around... Similar to the one I had earlier.

Issue: I want to crossfade from one DrawTexture to another.

My system: My system works with an XML parser. Basically, all the GUI items are in the same script.

For instance, my DrawTexture is being drawn in the GUI, like so:

GUI.DrawTexture (Rect (0, 0, 1280, 1024), textureBackground, ScaleMode.StretchToFill);

but changed by this command when the XML parser detects the tag :

textureBackground =  Resources.Load("Backgrounds/" + input.change,Texture2D);

the "input.change" is the name of the texture (which I state in the XML file like so: example01.

So what I'd like to do is sort of crossfade into the next texture as soon as I give that command.

My idea was, in layman's terms, to first make a second DrawTexture, which assumes the new texture when the command is given. Then I'd fade the alpha up to completely opaque (in say 2 seconds time), set the first DrawTexture to match the second DrawTexture, and then reset the second one's alpha back to 0, so the background image is now set. And of course repeat the process when the texture changes again.

I realize this is probably very amateuristic, but looking around UnityAnswers, I can't seem to find another way of doing it. All other Answers I find mostly use external script, and I can't, as I need the depth to stay the same (it is a background after all, there are many more GUI elements being drawn on top of it). Nor can I seem to get it working.

Please help me out, I've tried to do it myself but I'm just too much of a beginner/bad scripter to grasp this. Getting to the end of my project time, as well...


During the crossfade period you'll actually be drawing two textures, so just give yourself another reference and set a timer, e.g.:

var textureBackground : Texture2D;
var previousBackground : Texture2D;
public var crossfadeDuration : float = 2.0;
var crossfadeEnd : float;

function OnGUI()
  var oldColor : Color = GUI.color;
  var alpha : float = 1;

  if( crossfadeEnd > Time.time )
    alpha = 1.0 - (( crossfadeEnd - Time.time ) / crossfadeDuration);
    if( previousBackground != null )
      GUI.color = Color( 1, 1, 1, 1 - alpha );
      GUI.DrawTexture (Rect (0, 0, 1280, 1024), previousBackground, ScaleMode.StretchToFill);

  GUI.color = Color( 1, 1, 1, alpha );
  GUI.DrawTexture (Rect (0, 0, 1280, 1024), textureBackground, ScaleMode.StretchToFill);

  GUI.color = oldColor;
  // other controls that are in front of backgrounds

function NewBackground()
  // do your loading, etc, and start the timer, e.g.
  previousBackground = textureBackground;
  textureBackground =  Resources.Load("Backgrounds/" + input.change,Texture2D);
  crossfadeEnd = Time.time + crossfadeDuration;

(note: this is off-the-cuff, and I don't usually use JS, but it should get you in the right direction).

Is there a way to do this using a 3D plane with a texture instead of GUI elements?

You can do this easily with a custom shader and a simple script: http://www.sundh.com/blog/2012/09/real-time-blend-2-textures-in-unity/