RenderTexture Free

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Changed the parameters. You can now control the x and y of the destination.)
(RenderTextureFree.cs: tabs)
 
Line 30: Line 30:
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
using System.Collections;
 
  
 
/// <summary>
 
/// <summary>
Line 58: Line 57:
  
 
// That's the heavy part, it takes a lot of time.
 
// That's the heavy part, it takes a lot of time.
                result.Apply();
+
result.Apply();
  
 
return result;
 
return result;
 
}
 
}
 
}</syntaxhighlight>
 
}</syntaxhighlight>

Latest revision as of 01:45, 18 June 2012

[edit] Description

Simulate a render texture by copying what is displayed on screen into a texture, GUI included. A Texture.Apply() is performed, which is extremely slow, so it is a bad idea to capture the screen every frames.

[edit] Usage

Attach the following script to the object receiving the texture.

using UnityEngine;
using System.Collections;
 
public class RTtest : MonoBehaviour 
{	
	public float refresh = 1f;
 
	// Update is called once per frame
	void Start (){
		InvokeRepeating( "SimulateRenderTexure", 0f, refresh );
	}
 
	void SimulateRenderTexure(){		
		renderer.material.mainTexture = RenderTextureFree.Capture();
	}
}

[edit] RenderTextureFree.cs

using UnityEngine;
 
/// <summary>
/// Simulate a RenderTexture. The process need a Texture.Apply, which takes a lot of time,
/// so don't use this every frames.
///
/// by Berenger Mantoue, www.berengermantoue.fr
///
/// </summary>
 
public class RenderTextureFree
{
	private static Texture2D tex2D;
	private static Texture tex;
 
	// Return the entire screen in a texture
	public static Texture Capture(){ return Capture( new Rect( 0f, 0f, Screen.width, Screen.height ), 0, 0 ); }
 
	// Return part of the screen in a texture.
	public static Texture Capture( Rect captureZone, int destX, int destY )
	{
		Texture2D result;
		result = new Texture2D( Mathf.RoundToInt( captureZone.width ) + destX,
					Mathf.RoundToInt( captureZone.height ) + destY,
					TextureFormat.RGB24, false);
		result.ReadPixels(captureZone, destX, destY, false);
 
		// That's the heavy part, it takes a lot of time.
		result.Apply();
 
		return result;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox