Last active
December 30, 2015 06:28
-
-
Save tsubaki/7789119 to your computer and use it in GitHub Desktop.
擬似レンダーテクスチャ。(デモ:https://dl.dropboxusercontent.com/u/56297224/UnitySumple2/offscreen%20render%28free%29/Capture%20Color/Capture%20Color.html)
This file contains 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 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); | |
} | |
} | |
} |
This file contains 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 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