本文整理汇总了C#中UnityEngine.Texture2D.ReadPixels方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.ReadPixels方法的具体用法?C# Texture2D.ReadPixels怎么用?C# Texture2D.ReadPixels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.ReadPixels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnInspectorGUI
public override void OnInspectorGUI()
{
if (GUILayout.Button("Generate Thumb"))
{
Camera cam = (Camera)target;
CameraClearFlags clearFlags = cam.clearFlags;
cam.clearFlags = CameraClearFlags.Depth;
RenderTexture renTex = new RenderTexture(size, size, 1);
cam.targetTexture = renTex;
cam.Render();
cam.targetTexture = null;
Texture2D tex = new Texture2D(size, size);
RenderTexture.active = renTex;
tex.ReadPixels(new Rect(0, 0, size, size), 0, 0);
RenderTexture.active = null;
byte[] data = tex.EncodeToPNG();
string scenePath = Path.GetFullPath(Path.GetDirectoryName(EditorSceneManager.GetActiveScene().path));
string sceneName = EditorSceneManager.GetActiveScene().name;
string file = scenePath + "/Thumbnails/" + sceneName + ".png";
File.WriteAllBytes(file, data);
Debug.Log("Thumbnail saved to: " + file);
cam.clearFlags = clearFlags;
}
base.OnInspectorGUI();
}
示例2: ScreenshotEncode
IEnumerator ScreenshotEncode()
{
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
// save our test image (could also upload to WWW)
string filePath = Application.dataPath + "/screenshots/" + KSMetric.List[GraphAxis.me.xLabel].Name + "_"
+ KSMetric.List[GraphAxis.me.yLabel].Name + "/"
+ GraphAxis.me.legendTitles[GraphAxis.me.selectedLegend] + ".png";
FileInfo file = new System.IO.FileInfo(filePath);
file.Directory.Create(); // If the directory already exists, this method does nothing.
File.WriteAllBytes(file.FullName, bytes);
//File.WriteAllBytes(filePath, bytes);
//File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
count++;
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject( texture );
//Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
}
示例3: ScreenshotEncode
IEnumerator ScreenshotEncode()
{
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
count = PlayerPrefs.GetInt("count");
// save our test image (could also upload to WWW)
File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
count++;
PlayerPrefs.SetInt("count", count);
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject(texture);
Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
}
示例4: OnRenderImage
void OnRenderImage (RenderTexture source, RenderTexture destination){
Graphics.Blit(source,destination,mat);
//mat is the material which contains the shader
//we are passing the destination RenderTexture to
int width = Screen.width;
int height = Screen.height;
Debug.Log (width.ToString ());
Debug.Log (height.ToString ());
Texture2D tex = new Texture2D(width, height);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
if (flag == true) {
string[] blue = new string[width * height];
for (int i = 0; i < height; i++) {
for (int j=0; j < width; j++) {
blue[(i * width) + j] = tex.GetPixel (j, i).b.ToString ();
}
}
string output = string.Join(",", blue);
File.WriteAllText ("/Users/mettinger/Desktop/temp/output.txt", output);
flag = false;
}
Debug.Log (tex.GetPixel(x,y).b.ToString());
}
示例5: CaptureCamera
/// <summary>
/// 对相机截图。
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="camera">Camera.要被截屏的相机</param>
/// <param name="rect">Rect.截屏的区域</param>
public static Texture2D CaptureCamera(string _fileParentPath, string _fileName)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)Screen.width, (int)Screen.height, 24);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
Camera.main.targetTexture = rt;
Camera.main.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
screenShot = new Texture2D((int)Screen.width * 3 / 4, (int)Screen.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect(Screen.width * 1 / 8, 0, Screen.width * 3 / 4, Screen.height), 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
Camera.main.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个png图片文件
//byte[] bytes = screenShot.EncodeToPNG();
byte[] bytes = screenShot.EncodeToJPG();
string filename = _fileParentPath + _fileName;
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
示例6: Screenshot
IEnumerator Screenshot()
{
if (!Directory.Exists(outPath))
{
Directory.CreateDirectory(outPath);
}
while (record)
{
/*counter++;
Application.CaptureScreenshot(outPath + counter.ToString("D8") + ".png", 0);
UnityEngine.Debug.Log("PNG Created " + counter);
yield return new WaitForSeconds(recordStep);*/
counter++;
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camera.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = outPath + counter.ToString("D8") + ".png";//8 didget naming scheme limits us to videos roughly 28.9 days long
System.IO.File.WriteAllBytes(filename, bytes);
UnityEngine.Debug.Log(string.Format("Took screenshot to: {0}", filename));
yield return new WaitForSeconds(recordStep);
}
yield break;
}
示例7: LateUpdate
void LateUpdate()
{
takeHiResShot |= Input.GetKeyDown("joystick button 0");
if (takeHiResShot)
{
if (photos.Count >= 5)
{
Debug.Log("Too many photos, not saving");
takeHiResShot = false;
return;
}
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camera.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
photos.Add(bytes);
takeHiResShot = false;
Debug.Log(string.Format("Added screenshot to memory. {0} photos in total now.", photos.Count));
Texture2D texture = new Texture2D(resWidth, resHeight);
texture.LoadImage(bytes);
}
}
示例8: GeneratePreview
public static Texture2D GeneratePreview(Object obj, int width, int height)
{
Texture2D tex = null;
GameObject go = obj as GameObject;
if(PrepareCamera(previewCamera, go, width, height))
{
go = GameObject.Instantiate(go);
go.transform.position = Vector3.zero;
RenderTexture renderTexture = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default, 1);
RenderTexture.active = renderTexture;
previewCamera.targetTexture = renderTexture;
previewCamera.Render();
tex = new Texture2D(width, height);
tex.ReadPixels(new Rect(0,0,renderTexture.width,renderTexture.height), 0, 0);
tex.Apply();
RenderTexture.ReleaseTemporary(renderTexture);
pb_ObjectUtility.Destroy(go);
}
return tex;
}
示例9: Screenshot
public static Texture2D Screenshot(Rect rect)
{
Camera camera = Camera.main;
//Camera camera =
//create temp textures, one for the camera to render
RenderTexture renderTexture = new RenderTexture((int)rect.width, (int)rect.height, 16, RenderTextureFormat.ARGB32);
//and one for the file
Texture2D texture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
//Texture2D texture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
//set renderTexture
RenderTexture.active = renderTexture;
RenderTexture oldTexture = Camera.main.targetTexture;
camera.targetTexture = renderTexture;
camera.Render();
//read the content of the camera into the texture
texture.ReadPixels(rect, 0, 0);
//texture.Apply();
//release the renders
RenderTexture.active = null;
//camera.targetTexture = null;
camera.targetTexture = oldTexture;
return texture;
}
示例10: ApplyShare
IEnumerator ApplyShare()
{
shareToFBPanel_anim.SetBool ("show", false);
string message = transform.parent.Find ("Message").Find ("Text").GetComponent<Text> ().text;
yield return new WaitForSeconds(.5f);
#if UNITY_EDITOR
Debug.Log ("Capture screenshot and share.");
Debug.Log ("Message: "+message);
GameObject.Find ("ResultPanel").SendMessage("ShareToFB", false);
#elif UNITY_ANDROID
var snap = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
snap.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
snap.Apply();
var screenshot = snap.EncodeToPNG();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "screenshot.png");
wwwForm.AddField("message", message);
FB.API(
"/me/photos",
Facebook.HttpMethod.POST,
delegate (FBResult r) {
GameObject.Find("ResultPanel").SendMessage("ShareToFB", false);
},
wwwForm
);
#endif
}
示例11: UploadJPG
IEnumerator UploadJPG()
{
// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
// Create a Web Form
WWWForm form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("fileUpload", bytes, @"C:\images\img_file.jpg", "image/jpg");
// Upload to a cgi script
WWW w = new WWW(url, form);
yield return w;
if (!string.IsNullOrEmpty(w.error)) {
print(w.error);
}
else {
print("Finished Uploading Screenshot");
}
}
示例12: TakeScreenShot
// return file name
public IEnumerator TakeScreenShot()
{
yield return new WaitForEndOfFrame();
Camera camOV = OVcamera.GetComponent<Camera>();
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = camOV.targetTexture;
camOV.Render();
Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
imageOverview.Apply();
RenderTexture.active = currentRT;
// Encode texture into PNG
byte[] bytes = imageOverview.EncodeToPNG();
// save in memory
path = Application.persistentDataPath + "/Screenshot.png";
Debug.Log (path);
System.IO.File.WriteAllBytes(path, bytes);
}
示例13: RenderCamera
public void RenderCamera(int resWidth, int resHeight, string path){
// Set Camera
Camera.main.orthographic = true;
Camera.main.orthographicSize = MapSize * 0.05f;
Pivot.localPosition = new Vector3(MapSize * 0.05f, 0, -MapSize * 0.05f);
Pivot.rotation = Quaternion.identity;
// Take Screenshoot
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
Camera.main.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
Camera.main.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
Camera.main.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes;
if(path.Contains(".png")) bytes = screenShot.EncodeToPNG();
else bytes = screenShot.EncodeToJPG();
System.IO.File.WriteAllBytes(path, bytes);
// Restart Camera
Camera.main.orthographic = false;
RestartCam();
}
示例14: ReadPixels
/* ----------------------------------------
* A coroutine where the screenshot is taken according to the preferences
* set by the user
*/
IEnumerator ReadPixels()
{
// bytes array for converting pixels to image format
byte[] bytes;
// Wait for the end of the frame, so GUI elements are included in the screenshot
yield return new WaitForEndOfFrame();
// Create new Texture2D variable of the same size as the image capture area
texture = new Texture2D (sw,sh,TextureFormat.RGB24,false);
// Read Pixels from the capture area
texture.ReadPixels(sRect,0,0);
// Apply pixels read com capture area into 'texture'
texture.Apply();
// IF selected method is 'ReadPixelsJpg'...
if (captMethod == method.ReadPixelsJpg){
// store as bytes the texture encoded to JPG (using 'jpgQuality' as quality settings)
bytes = texture.EncodeToJPG(jpgQuality);
WriteBytesToFile(bytes, ".jpg");
} else if (captMethod == method.ReadPixelsPng){
// store as bytes the texture encoded to PNG
bytes = texture.EncodeToPNG();
WriteBytesToFile(bytes, ".png");
}
}
示例15: Encode
public static void Encode(Texture2D src, Texture2D dst, bool gpu = false,
YCgACoFormat format = YCgACoFormat.CgACoY_DontChange,
int quality = 100)
{
var pixels = src.GetPixels();
Resize(src, dst, format);
var resized = src.width != dst.width || src.height != dst.height;
if (gpu)
{
// TODO: Force mipmap and trilinear when resized.
var shader = Shader.Find("Hidden/RGBA to CgACoY");
var mat = new Material(shader);
var temp = RenderTexture.GetTemporary(dst.width, dst.height);
Graphics.Blit(src, temp, mat);
dst.ReadPixels(new Rect(0, 0, dst.width, dst.height), 0, 0);
RenderTexture.ReleaseTemporary(temp);
Object.DestroyImmediate(mat);
}
else
{
if (resized)
{
var srcPixels = pixels;
pixels = dst.GetPixels();
Shrink(srcPixels, pixels, src.width, dst.width);
}
RGBAToCgACoY(pixels, pixels);
dst.SetPixels(pixels);
}
Compress(dst, format, quality);
}