Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

static var active: RenderTexture;
static RenderTexture active;
static active as RenderTexture

Description

The active render texture.

All rendering goes into the active RenderTexture. If the active RenderTexture is null everything is rendered in the main window.

When a RenderTexture becomes active its hardware rendering context is automatically created if it hasn't been created already.

// Get the contents of a RenderTexture into a Texture2D.
function GetRTPixels(rt: RenderTexture) {
	// Keep a note of the currently active RenderTexture.
	var currentActiveRT = RenderTexture.active;
	
	// Set the supplied RenderTexture as the active one.
	RenderTexture.active = rt;
	
	// Create a new Texture2D and read the RenderTexture image
	// into it.
	var tex = new Texture2D(rt.width, rt.height);
	tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
	
	// Set the previously active RenderTexture back before returning.
	RenderTexture.active = currentActiveRT;
	return tex;
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    Texture2D GetRTPixels(RenderTexture rt) {
        RenderTexture currentActiveRT = RenderTexture.active;
        RenderTexture.active = rt;
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
        RenderTexture.active = currentActiveRT;
        return tex;
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def GetRTPixels(rt as RenderTexture) as Texture2D:
		currentActiveRT as RenderTexture = RenderTexture.active
		RenderTexture.active = rt
		tex as Texture2D = Texture2D(rt.width, rt.height)
		tex.ReadPixels(Rect(0, 0, tex.width, tex.height), 0, 0)
		RenderTexture.active = currentActiveRT
		return tex