popcorn/Scripts/Utilities/CaptchaRenderTexture.cs

28 lines
837 B
C#

using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CaptchaRenderTexture : MonoBehaviour {
private Camera cam;
void Awake(){
cam = GetComponent<Camera>();
}
public Texture2D CaptchaToTexture2D(int width, int height, int depth, bool linear = false, bool mipmap = false, TextureFormat format = TextureFormat.ARGB32){
RenderTexture renderTexture = new RenderTexture(width, height, depth);
renderTexture.Create();
cam.targetTexture = renderTexture;
cam.Render();
Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height, format, mipmap, linear);
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0, mipmap);
texture.Apply();
cam.targetTexture = null;
RenderTexture.active = null;
return texture;
}
}