找回密码
 立即注册
查看: 365|回复: 0

Unity 截屏功能

[复制链接]
发表于 2022-6-4 16:01 | 显示全部楼层 |阅读模式
原文链接 https://zhuanlan.zhihu.com/p/102158263
使用方法


image.png

    1、选择相机2、设置保存位置3、截图
具体代码

using System.IO;using UnityEditor;using UnityEngine;public class ScreenShotWindow : EditorWindow{    private Camera m_Camera;    private string filePath;    private bool m_IsEnableAlpha = false;    private CameraClearFlags m_CameraClearFlags;        [MenuItem("Tools/屏幕截图")]    private static void Init()    {        ScreenShotWindow window = GetWindowWithRect<ScreenShotWindow>(new Rect(0, 0, 300, 150));        window.titleContent = new GUIContent("屏幕截图");        window.Show();    }    private void OnGUI()    {        EditorGUILayout.Space();        m_Camera = EditorGUILayout.ObjectField("选择摄像机", m_Camera, typeof(Camera), true) as Camera;        if (GUILayout.Button("保存位置"))        {            filePath = EditorUtility.OpenFolderPanel("", "", "");        }                m_IsEnableAlpha = EditorGUILayout.Toggle("是否开启透明通道", m_IsEnableAlpha);        EditorGUILayout.Space();        if (GUILayout.Button("截图"))        {            TakeShot();        }        EditorGUILayout.Space();        if (GUILayout.Button("打开导出文件夹"))        {            if (string.IsNullOrEmpty(filePath))            {                Debug.LogError("<color=red>" + "没有选择截图保存位置" + "</color>");                return;            }            Application.OpenURL("file://" + filePath);        }    }        private void TakeShot()    {        if (m_Camera == null)        {            Debug.LogError("<color=red>" + "没有选择摄像机" + "</color>");            return;        }        if (string.IsNullOrEmpty(filePath))        {            Debug.LogError("<color=red>" + "没有选择截图保存位置" + "</color>");            return;        }                m_CameraClearFlags = m_Camera.clearFlags;        if (m_IsEnableAlpha)        {            m_Camera.clearFlags = CameraClearFlags.Depth;        }         int resolutionX = (int)Handles.GetMainGameViewSize().x;        int resolutionY = (int)Handles.GetMainGameViewSize().y;        RenderTexture rt = new RenderTexture(resolutionX, resolutionY, 24);        m_Camera.targetTexture = rt;        Texture2D screenShot = new Texture2D(resolutionX, resolutionY, TextureFormat.ARGB32, false);        m_Camera.Render();        RenderTexture.active = rt;        screenShot.ReadPixels(new Rect(0, 0, resolutionX, resolutionY), 0, 0);        m_Camera.targetTexture = null;        RenderTexture.active = null;        m_Camera.clearFlags = m_CameraClearFlags;        //Destroy(rt);        byte[] bytes = screenShot.EncodeToPNG();        string fileName = filePath + "/" + $"{System.DateTime.Now:yyyy-MM-dd_HH-mm-ss}" + ".png";        File.WriteAllBytes(fileName, bytes);        Debug.Log("截图成功");    }}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 09:33 , Processed in 0.088038 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表