Untiy工具分享-截图工具

Untiy实现截图Game视图工具 方便快捷 不再依赖微信/QQ或者其他截图工具进行截图 并且可以自定义路径和分辨率!!!

下面是代码:

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,
            alignment = TextAnchor.MiddleCenter,
            normal = new GUIStyleState() { textColor = Color.green }
        };
    }

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

        GUILayout.Label("游戏窗口截图", BigText);
        if (GUILayout.Button("截图"))
        {
            TakeScreenshot(1);
        }
        GUILayout.Label("分辨率: " + GetResolution(1));

        if (GUILayout.Button("双倍分辨率截图"))
        {
            TakeScreenshot(2);
        }
        GUILayout.Label("分辨率: " + GetResolution(2));

        if (GUILayout.Button("打开目录"))
        {
            ShowFolder();
        }
        GUILayout.Label("保存目录: " + directory);
    }

    [MenuItem("Tools/截图")]
    public static void ShowWindow()
    {
        var window = GetWindowWithRect<ScreenCaptureEditor>(new Rect(0, 0, 180, 250));
        window.titleContent = new GUIContent("截图工具");
        window.Focus();
        window.Repaint();
    }

    private static void ShowFolder()
    {
        if (File.Exists(latestScreenshotPath))
        {
            EditorUtility.RevealInFinder(latestScreenshotPath);
            return;
        }
        Directory.CreateDirectory(directory);
        EditorUtility.RevealInFinder(directory);
    } 

    private static void TakeScreenshot(int super)
    {
        Directory.CreateDirectory(directory);
        var currentTime = System.DateTime.Now;
        var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png";
        var path = directory + filename;
        ScreenCapture.CaptureScreenshot(path, super);
        latestScreenshotPath = path;
        Debug.Log($"Screenshot saved: <b>{path}</b> with resolution <b>{GetResolution(super)}</b>");
    }

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值