Как сделать снимок экрана в определенной области в единстве - PullRequest
0 голосов
/ 11 января 2020

Я использую 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);
 }
 }

это мой код выше.

1 Ответ

0 голосов
/ 15 января 2020

используйте это:

    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);
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...