Last active
March 1, 2018 10:31
-
-
Save sagarpatel/2f81b29dbd470214d5351a8c08c996d3 to your computer and use it in GitHub Desktop.
Capture Screenshot And Apply Texture To Material
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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