Unity 四种截图方法(相机视图、无UI、有UI、Game窗口)

之前介绍了三种截图方法(全屏截图、自定义大小截图、无UI截图)

今天介绍一下在Unity 编辑器中进行截图,这种截图的方式是截取Game窗口视角,在编辑器内可以随时使用,而且还可以设置快捷键

 

 脚本如下:由于此脚本继承的是EditorWindow,使用需要放入Editor文件夹内

using System.IO;
using UnityEditor;
using UnityEngine;

public class ScreenCaptureEditor : EditorWindow
{
    private static string directory = "Screenshots/Capture/";
    private static string latestScreenshotPath = "";
    private bool initDone = false;

    private GUIStyle BigText;

    void InitStyles()
    {
        initDone = true;
        BigText = new GUIStyle(GUI.skin.label)
        {
            fontSize = 20,
            fontStyle = FontStyle.Bold
        };
    }

    private void OnGUI()
    {
        if (!initDone)
        {
            InitStyles();
        }

        GUILayout.Label("Screen Capture Tools", BigText);
        if (GUILayout.Button("单张截图"))
        {
            TakeScreenshot();
        }
        GUILayout.Label("当前分辨率: " + GetResolution());

        if (GUILayout.Button("打开文件夹"))
        {
            ShowFolder();
        }
        GUILayout.Label("保存路径: " + directory);
    }

    [MenuItem("Tools/Screenshots/打开窗口 &`", false, 0)]
    public static void ShowWindow()
    {
        GetWindow(typeof(ScreenCaptureEditor));
    }

    [MenuItem("Tools/Screenshots/存储路径 &2", false, 2)]
    private static void ShowFolder()
    {
        if (File.Exists(latestScreenshotPath))
        {
            EditorUtility.RevealInFinder(latestScreenshotPath);
            return;
        }
        Directory.CreateDirectory(directory);
        EditorUtility.RevealInFinder(directory);
    }

    [MenuItem("Tools/Screenshots/单张截图 &1", false, 1)]
    private static void TakeScreenshot()
    {
        Directory.CreateDirectory(directory);
        var currentTime = System.DateTime.Now;
        var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png";
        var path = directory + filename;
        ScreenCapture.CaptureScreenshot(path);
        latestScreenshotPath = path;
        Debug.Log($"截图路径: <b>{path}</b> 分辨率: <b>{GetResolution()}</b>");
    }

    private static string GetResolution()
    {
        Vector2 size = Handles.GetMainGameViewSize();
        Vector2Int sizeInt = new Vector2Int((int)size.x, (int)size.y);
        return $"{sizeInt.x}x{sizeInt.y}";
    }

}

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值