Unity调用系统选择文件功能实现文件的选择及另存为

1.系统文件选择结构体

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using System.Collections;

/// <summary>
/// windows系统文件选择窗口
/// </summary>
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Auto)]
public struct OpenFileName
{
    public int structSize;
    public IntPtr dlgOwner;
    public IntPtr instance;
    public String filter;
    public String customFilter;
    public int maxCustFilter;
    public int filterIndex;
    public String file;
    public int maxFile;
    public String fileTitle;
    public int maxFileTitle;
    public String initialDir;
    public String title;
    public int flags;
    public short fileOffset;
    public short fileExtension;
    public String defExt;
    public IntPtr custData;
    public IntPtr hook;
    public String templateName;
    public IntPtr reservedPtr;
    public int reservedInt;
    public int flagsEx;
}
public class WindowDll
{
    //链接指定系统函数       打开文件对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    public static bool GetOFN([In, Out] OpenFileName ofn)
    {
        return GetOpenFileName(ofn);
    }

    //链接指定系统函数        另存为对话框
    [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);
    }
}

2.调用案例

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class OpenFileNameExample : MonoBehaviour
{
    public Button choose;
    private void Awake()
    {
        choose.onClick.AddListener(() => 
        {
            //注意存入路径是否有效
            ChooseFileAndSaveNewPath(Application.streamingAssetsPath + "/图片");
        });
    }
    /// <summary>
    /// 选择文件并存入新的地址
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool ChooseFileAndSaveNewPath(string path)
    {
        OpenFileName openFileName = new OpenFileName();
        openFileName.structSize = Marshal.SizeOf(openFileName);
        openFileName.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
        openFileName.file = new string(new char[1024]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        openFileName.title = "窗口标题";
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

        if (WindowDll.GetSaveFileName(openFileName))
        {
            string oldPath=openFileName.file.Replace("\\", "/");
            if (oldPath.Substring(oldPath.Length - 4) == ".JPG")
                path += ".JPG";
            else
                path += ".PNG";
            for (int i = oldPath.Length - 1; i > 0; i--)
            {
                if (oldPath[i] == 'G' || oldPath[i] == 'g')
                    break;
                oldPath = oldPath.Remove(i);
            }
            File.Copy(oldPath, path, true);
            return true;
        }
        return false;
    }
}

File类在copy文件时文件地址绝对不能有一点错误!!!在定义结构体时为了尽量满足超长路径,所以将openFileName.file的长度尽可能设定得更长。但是当你选择文件之后返回的地址并不合规,他会将不足设定长度的地址补足位数,补足的位数值为空导致打印出的地址和真实地址一模一样,所以我对其空字符做了删减处理。

3.实例效果如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值