Skip to content

Instantly share code, notes, and snippets.

@peroon
Created June 29, 2015 08:05
Show Gist options
  • Save peroon/4d316129f8264a5e897b to your computer and use it in GitHub Desktop.
Save peroon/4d316129f8264a5e897b to your computer and use it in GitHub Desktop.
[Unity]Take ScreenShot of GameView and assign it as material texture
using UnityEngine;
using System.Collections;
using System.IO;
public class Temp : MonoBehaviour {
void Start () {
StartCoroutine (this.TakeScreenShot ());
}
IEnumerator TakeScreenShot(){
// Screenshot を撮る
string path = Application.dataPath + "/screenshot.png";
Application.CaptureScreenshot(path);
Debug.Log("path:"+path);
// 即座に保存されるわけではないので待ち
while(!System.IO.File.Exists(path)){
print ("Saving");
yield return null;
}
// スクリーンショットの読み込み
byte[] image = File.ReadAllBytes(path);
// Texture2D を作成して読み込み
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(image);
GameObject.Find ("Cube").GetComponent<Renderer> ().material.mainTexture = tex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment