首页 > 解决方案 > 如何统一在特定区域进行屏幕截图

问题描述

我正在使用Unity 2018。在我的项目中,我必须采用特定区域的屏幕。我使用了下面的代码。这是工作。但是确切的图像不起作用。它在某种程度上。我怎样才能拍摄准确的图像。

   using UnityEngine;
   using System.Collections;
   using System;

  public class ScreenCapture : MonoBehaviour
  {
  public RenderTexture overviewTexture;
  GameObject OVcamera;
  public string path = "";

  void Start()
  {
  OVcamera = GameObject.FindGameObjectWithTag("OverviewCamera");
  }
  void LateUpdate()
 {           
 if (Input.GetKeyDown("f9"))
 {
 StartCoroutine(TakeScreenShot());
 }    
 }
// return file name
 string fileName(int width, int height)
{
 return string.Format("screen_{0}x{1}_{2}.png",
 width, height,
 System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
 }

 public IEnumerator TakeScreenShot()
 {
 yield return new WaitForEndOfFrame();

 Camera camOV = OVcamera.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;           
 byte[] bytes = imageOverview.EncodeToPNG();

     // save in memory
 string filename = fileName(Convert.ToInt32(imageOverview.width), 
 Convert.ToInt32(imageOverview.height));
 path = Application.persistentDataPath + "/Snapshots/" + filename;       
 System.IO.File.WriteAllBytes(path, bytes);
 }
 }

这是我上面的代码..

标签: c#unity3d

解决方案


用这个:

    Texture2D screencap;
    Texture2D border;
    bool shot=false;
    public string path;

    void Start () {
         screencap=new Texture2D(300,200,TextureFormat.RGB24,false);
         border=new Texture2D(2,2,TextureFormat.ARGB32,false);
         border.Apply();
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetKeyUp(KeyCode.Mouse0))
        {
            StartCoroutine("Capture");
        }

    }
    string fileName(int width, int height)
    {
        return string.Format("screen_{0}x{1}_{2}.png",
                             width, height,
                             System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    }

    void OnGUI()
    {
        GUI.DrawTexture(new Rect(200,100,300,2),border,ScaleMode.StretchToFill);
        GUI.DrawTexture(new Rect(200,300,300,2),border,ScaleMode.StretchToFill);
        GUI.DrawTexture(new Rect(195,100,2,200),border,ScaleMode.StretchToFill);
        GUI.DrawTexture(new Rect(500,100,2,201),border,ScaleMode.StretchToFill);

        if(shot)
        {
            GUI.DrawTexture(new Rect(50,10,60,40),screencap,ScaleMode.StretchToFill);
            //Application.CaptureScreenshot(myFolderLocation+myFilename);
        }
    }

    IEnumerator Capture()
    {
        yield return new WaitForEndOfFrame();
        screencap.ReadPixels(new Rect(198,98,298,198),0,0);
        screencap.Apply();
        shot=true;

        byte[] bytes=border.EncodeToPNG();
        string filename=fileName(Convert.ToInt32(screencap.width), Convert.ToInt32(screencap.height));
        Application.CaptureScreenshot("D:"+filename);
    }

推荐阅读