Skip to content

Instantly share code, notes, and snippets.

@sagarpatel
Last active March 1, 2018 10:31
Show Gist options
  • Save sagarpatel/2f81b29dbd470214d5351a8c08c996d3 to your computer and use it in GitHub Desktop.
Save sagarpatel/2f81b29dbd470214d5351a8c08c996d3 to your computer and use it in GitHub Desktop.
Capture Screenshot And Apply Texture To Material
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApplyScreenshotToMaterial : MonoBehaviour
{
public Material m_targetMaterial;
public string m_targetTextureName = "_MainTex";
Texture2D m_lastScreenshot;
void Start()
{
m_lastScreenshot = new Texture2D(Screen.width, Screen.height);
}
IEnumerator TakeScreenshotApplyToMat()
{
yield return new WaitForEndOfFrame();
m_lastScreenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
m_lastScreenshot.Apply();
m_targetMaterial.SetTexture(m_targetTextureName, m_lastScreenshot);
}
void LateUpdate()
{
if(Input.GetKeyDown(KeyCode.Space))
StartCoroutine(TakeScreenshotApplyToMat());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment