Unity功能实现_截图功能

目录

Unity功能实现_截图功能

带UI截图(全屏幕)

Unity内置方法截图:ScreenCapture

Texture2D截图方式

无UI截图(全屏幕)

指定区域截图


Unity功能实现_截图功能

截图功能从内容上分为两种情况:第一种是带UI截图,第二种是无UI截图

带UI截图(全屏幕)

Unity内置方法截图:ScreenCapture

直接输出到指定文件目录下:

using System;
using System.Collections;
using UnityEngine;
using System.IO;

public class ShootScreenTool : MonoBehaviour
{

    private string path = @"C:\Users\Admin\Desktop\Test";

    private void Start()
    {
        CheckPath();
    }

    private void CheckPath()
    {
        bool isExists = Directory.Exists(path);
        if (!isExists)
        {
            Directory.CreateDirectory(path);
            Debug.Log("不存在当前路径,创建文件夹");
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {

            StartCoroutine(ShootScreen());
        }
    }

    private IEnumerator ShootScreen()
    {
        yield return new WaitForEndOfFrame();
        string outPutPath = string.Concat(path, @"\" + DateTime.Now.ToString("yyyy.MM.dd_HH.mm") + "_ShootScreen.PNG");
        ScreenCapture.CaptureScreenshot(outPutPath);
        Debug.Log("截图路径为:" + outPutPath);
    }
}

将截图变成Texture2D:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class ShootScreenTool : MonoBehaviour
{

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {

            StartCoroutine(ShootScreen());
        }
    }
    private IEnumerator ShootScreen()
    {
        yield return new WaitForEndOfFrame();
        Texture2D tt = ScreenCapture.CaptureScreenshotAsTexture();
        GameObject.Find("RawImage").GetComponent().texture = tt;

    }

}

Texture2D截图方式

using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class ShootScreenTool : MonoBehaviour
{

    private string path = @"C:\Users\Admin\Desktop\Test";

    private void Start()
    {
        CheckPath();
    }

    private void CheckPath()
    {
        bool isExists = Directory.Exists(path);
        if (!isExists)
        {
            Directory.CreateDirectory(path);
            Debug.Log("不存在当前路径,创建文件夹");
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {

            StartCoroutine(ShootScreen());
        }
    }
    private IEnumerator ShootScreen()
    {
        yield return new WaitForEndOfFrame();

        int width = Screen.width;
        int height = Screen.height;

        Texture2D tt = new Texture2D(width, height);
        tt.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tt.Apply();

        byte[] data_PNG = tt.EncodeToPNG();//还有其它的格式:tt.EncodeToJPG(); tt.EncodeToTGA(); tt.EncodeToEXR();

        string outPutPath = string.Concat(path, @"\" + DateTime.Now.ToString("yyyy.MM.dd_HH.mm") + "_ShootScreen.PNG");
        File.WriteAllBytes(outPutPath, data_PNG);
    }

}

无UI截图(全屏幕)

using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class ShootScreenTool : MonoBehaviour
{

    private string path = @"C:\Users\Admin\Desktop\Test";

    private void Start()
    {
        CheckPath();
    }

    private void CheckPath()
    {
        bool isExists = Directory.Exists(path);
        if (!isExists)
        {
            Directory.CreateDirectory(path);
            Debug.Log("不存在当前路径,创建文件夹");
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {

            StartCoroutine(ShootScreen());
        }
    }
    private IEnumerator ShootScreen()
    {
        yield return new WaitForEndOfFrame();

        //创建一个RenderTexture对象
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 0);
        //将主Camera的图像渲染到RenderTexture上 

        Camera camera = Camera.main;
        camera.targetTexture = rt;
        camera.Render();

        //激活RenderTexture
        RenderTexture.active = rt;


        Texture2D tt = new Texture2D(Screen.width, Screen.height);
        tt.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        tt.Apply();

        //重置相关参数,以使用camera继续在屏幕上显示  
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);

        byte[] data_PNG = tt.EncodeToPNG();//还有其它的格式:tt.EncodeToJPG(); tt.EncodeToTGA(); tt.EncodeToEXR();

        string outPutPath = string.Concat(path, @"\" + DateTime.Now.ToString("yyyy.MM.dd_HH.mm") + "_ShootScreen.PNG");
        File.WriteAllBytes(outPutPath, data_PNG);
    }

}

指定区域截图

using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class ShootScreenTool : MonoBehaviour
{

    private string path = @"C:\Users\Admin\Desktop\Test";

    private Vector2 mouseDownPos;
    private Vector2 mouseUpPos;
    private Rect rt;
    private void Start()
    {
        CheckPath();
    }

    private void CheckPath()
    {
        bool isExists = Directory.Exists(path);
        if (!isExists)
        {
            Directory.CreateDirectory(path);
            Debug.Log("不存在当前路径,创建文件夹");
        }
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("开始截图,松手结束");
            mouseDownPos = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("结束截图");
            mouseUpPos = Input.mousePosition;
            Vector2 leftDownPos;
            leftDownPos.x = Mathf.Min(mouseDownPos.x, mouseUpPos.x);
            leftDownPos.y = Mathf.Min(mouseDownPos.y, mouseUpPos.y);
            Vector2 size;
            size.x = Mathf.Abs(mouseDownPos.x - mouseUpPos.x);
            size.y = Mathf.Abs(mouseDownPos.y - mouseUpPos.y);
            rt = new Rect(leftDownPos, size);
            StartCoroutine(ShootScreen(rt));
        }

    }
    private IEnumerator ShootScreen(Rect rt)
    {
        yield return new WaitForEndOfFrame();

        int width = (int)rt.width;
        int height = (int)rt.height;

        Texture2D tt = new Texture2D(width, height);
        tt.ReadPixels(rt, 0, 0);
        tt.Apply();

        byte[] data_PNG = tt.EncodeToPNG();//还有其它的格式:tt.EncodeToJPG(); tt.EncodeToTGA(); tt.EncodeToEXR();

        string outPutPath = string.Concat(path, @"\" + DateTime.Now.ToString("yyyy.MM.dd_HH.mm") + "_ShootScreen.PNG");
        File.WriteAllBytes(outPutPath, data_PNG);
    }

}

你可能感兴趣的:(Unity功能实现,unity)