Skip to content

The ToTexture2D() Method

Narek Torosyan edited this page Dec 31, 2018 · 2 revisions

About

This method extends RenderTexture.

This method returns a Texture2D containing the RenderTexture's contents.

This method can be used for using Texture2D-specific methods (like saving as PNG) with a RenderTexture.

Example

// This script exports the texture from a camera to a PNG.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NUDev.PlusSharp;

public class ExportCamToPNG : MonoBehaviour {

    public RenderTexture cameraTex;
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.S)) {
        		Screenshot();
        	}
	}

    void Screenshot() {
        Texture2D temp = cameraTex.ToTexture2D();
        temp.SaveToPNG(Application.persistentDataPath + "/game.png");
    }
}

Clone this wiki locally