unity自定义截图

本文介绍了一种在Unity中自定义截图的方法,包括通过编辑器界面设置截图尺寸、保存路径和文件名,支持相机尺寸、屏幕分辨率和固定尺寸三种截图方式。通过挂载到相机的`CameraDefined`脚本,可以方便地保存高质量的纹理到指定位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEditor;
using UnityEngine;


[CanEditMultipleObjects]
[CustomEditor(typeof(CameraDefined))]
public class CameraEditor : Editor
{

    public override void OnInspectorGUI()
    {
        // 属性
        CameraDefined script = (CameraDefined)target;
        int selected = (int)script.captureSize;

        // GUI
        EditorGUI.BeginChangeCheck();
        drawProperty("targetCamera", "目标像机");

       //属性设置三种截图方式
        string[] options = new string[] { "像机", "屏幕", "自定义" };
        selected = EditorGUILayout.Popup("截图尺寸", selected, options, GUILayout.ExpandWidth(true));
        script.captureSize = (CameraDefined.CaptureSize)selected;
        if (script.captureSize == CameraDefined.CaptureSize.FixedSize)
        {
            drawProperty("pixelSize", "像素尺寸");
            EditorGUILayout.HelpBox("保持正确的宽高比!/n否则截图区域会出问题。", MessageType.Info);
        }
        drawProperty("savePath", "保存路径");
        drawProperty("fileName", "文件名");

        // 保存截图按钮
        bool isPress = GUILayout.Button("保存截图", GUILayout.ExpandWidth(true));
        if (isPress) script.saveCapture();
        if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
    }

    private void drawProperty(string property, string label)
    {
        EditorGUILayout.PropertyField(serializedObject.FindProperty(property), new GUIContent(label), true);
    }

}

using UnityEngine;
using System.IO;

///挂载到相机
public class CameraDefined: MonoBehaviour
{

    // 截图尺寸
    public enum CaptureSize
    {
        CameraSize,
        ScreenResolution,
        FixedSize
    }

    // 目标摄像机
    public Camera targetCamera;
    // 截图的尺寸
    public CaptureSize captureSize = CaptureSize.CameraSize;
    // 像素
    public Vector2 pixelSize;
    // 保存路径
    public string savePath = "StreamingAssets/";
    // 文件名称
    public string fileName = "cameradefined.png";
    private void Reset()
    {
        targetCamera = GetComponent<Camera>();
        pixelSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
    }

 //保存截图
    public void saveCapture()
    {
        Vector2 size = pixelSize;
        if (captureSize == CaptureSize.CameraSize)
        {
            size = new Vector2(targetCamera.pixelWidth, targetCamera.pixelHeight);
        }
        else if (captureSize == CaptureSize.ScreenResolution)
        {
            size = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
        }
        string path = Application.dataPath + "/" + savePath + fileName;
        saveTexture(path, capture(targetCamera, (int)size.x, (int)size.y));
    }

  //截图
    public static Texture2D capture(Camera camera)
    {
        return capture(camera, Screen.width, Screen.height);
    }


    public static Texture2D capture(Camera camera, int width, int height)
    {
        RenderTexture rt = new RenderTexture(width, height, 0);
        rt.depth = 24;
        rt.antiAliasing = 8;
        camera.targetTexture = rt;
        camera.RenderDontRestore();
        RenderTexture.active = rt;
        Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
        Rect rect = new Rect(0, 0, width, height);
        texture.ReadPixels(rect, 0, 0);
        texture.filterMode = FilterMode.Point;
        texture.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        //Destroy(rt);
        return texture;
    }

    //保存贴图 
  
    public static void saveTexture(string path, Texture2D texture)
    {
        File.WriteAllBytes(path, texture.EncodeToPNG());
        Debug.Log("已截屏并保存截图到:" + path);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Batman1208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值