Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active December 30, 2015 06:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/7789119 to your computer and use it in GitHub Desktop.
Save tsubaki/7789119 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class GetTexture : MonoBehaviour {
public BackgroundRenderBasic target;
public GameObject model = null;
void Start()
{
target.result = ()=>
{
renderer.material.mainTexture = target.screenshot;
};
}
void OnGUI()
{
if( GUILayout.Button("camera "+ ((target.gameObject.activeSelf)?"off":"on")) )
{
target.gameObject.SetActive(!target.gameObject.activeSelf);
}
if( GUILayout.Button("model "+ ((model.activeSelf)?"hide":"show")) )
{
model.SetActive(!model.activeSelf);
}
}
}
using UnityEngine;
using System.Collections;
using System.IO;
using System;
namespace AngryChicken2D
{
public class BackgroundRenderBasic : MonoBehaviour
{
public Texture2D screenshot { get; private set; }
RenderTexture renderTexture = null;
public Action result = null;
[Range(1, 5)]
public int
textureScale = 1;
void Awake()
{
int width = Screen.width / textureScale;
int height = Screen.height / textureScale;
screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
renderTexture = new RenderTexture(width, height, 24);
camera.targetTexture = renderTexture;
}
void OnPostRender()
{
Take();
if (result != null)
result();
gameObject.SetActive(false);
}
protected void Take()
{
screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenshot.Apply();
}
void OnDestroy()
{
Destroy(renderTexture);
Destroy(screenshot);
result = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment