您当前的位置: 首页 >  unity

十幺卜入

暂无认证

  • 1浏览

    0关注

    119博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d实现自由选中框并截图保存功能(三)选择路径保存截图功能

十幺卜入 发布时间:2021-03-26 11:20:42 ,浏览量:1

Unity3d实现自由选中框并截图保存功能(三)选择路径保存截图功能
  • 前言
  • 实现效果
    • 定义OpenFileName类
    • 定SelectFileDialog类
    • 选择文件
  • 项目源码

前言

前两篇实现了自由框选和截图保存的功能:

Unity3d实现自由选中框并截图保存功能(一)自由选中框实现 Unity3d实现自由选中框并截图保存功能(二)截图保存功能

本篇来实现用户手动选择截图保存路径以及文件名的功能,主要是使用System.Runtime.InteropServices内库来实现。

实现效果

在这里插入图片描述

保存的截图: 在这里插入图片描述

定义OpenFileName类

这个类是固定的结构,详细说明可以查看官方文档 OpenFileName详细参数 https://docs.microsoft.com/zh-cn/windows/win32/api/commdlg/ns-commdlg-openfilenamea?redirectedfrom=MSDN

主要用于GetSaveFileName的传参。

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    public int structSize = 0;
    public IntPtr dlgOwner = IntPtr.Zero;
    public IntPtr instance = IntPtr.Zero;
    public String filter = null;
    public String customFilter = null;
    public int maxCustFilter = 0;
    public int filterIndex = 0;
    public String file = null;
    public int maxFile = 0;
    public String fileTitle = null;
    public int maxFileTitle = 0;
    public String initialDir = null;
    public String title = null;
    public int flags = 0;
    public short fileOffset = 0;
    public short fileExtension = 0;
    public String defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public String templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}
定SelectFileDialog类

这个类主要定义了系统的函数。

using System.Runtime.InteropServices;

public class SelectFileDialog
{
    //系统函数  
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
    public static bool GetSFN([In, Out] OpenFileName ofn)
    {
        return GetSaveFileName(ofn);
    }
}
选择文件

新建OpenFileName对象并进行赋值。

 IEnumerator CutImage()
    {
        string Name = DateTime.Now.ToString("yyyyMMddHHmmss");

        Tex = new Texture2D((int)Mathf.Abs(StMsPos.x - EdMsPos.x), (int)Mathf.Abs(StMsPos.y - EdMsPos.y), TextureFormat.RGB24, true);

        CutRect = new Rect(StMsPos.x> EdMsPos.x? EdMsPos.x: StMsPos.x,
           StMsPos.y > EdMsPos.y ? EdMsPos.y : StMsPos.y,
           Mathf.Abs(EdMsPos.x - StMsPos.x), 
           Mathf.Abs(EdMsPos.y - StMsPos.y));

        yield return new WaitForEndOfFrame();
        Tex.ReadPixels(CutRect, 0, 0, true);
        Tex.Apply();
        yield return Tex;
        byte[] bytes = Tex.EncodeToPNG();

        string path = Application.dataPath + "/" + Name + ".png";

        OpenFileName file = new OpenFileName();
        file.structSize = Marshal.SizeOf(file);
        file.filter = "文件(*.png)";
        file.file = new string(new char[256]);
        file.maxFile = file.file.Length;
        file.fileTitle = new string(new char[64]);
        file.maxFileTitle = file.fileTitle.Length;
        file.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        file.title = "保存文件";
        file.templateName = Name;
        file.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

        if (SelectFileDialog.GetSaveFileName(file))
        {
            path = file.file + ".png";
        }
        File.WriteAllBytes(path, bytes);
    }
    

这里的filter属性定义了选择的范围必须是“png”后缀文件。


 file.filter = "文件(*.png)";
项目源码

https://download.csdn.net/download/qq_33789001/16092637

关注
打赏
1663314737
查看更多评论
立即登录/注册

微信扫码登录

0.0909s