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.实例效果如下
文章来源:https://www.toymoban.com/news/detail-726832.html
文章来源地址https://www.toymoban.com/news/detail-726832.html
到了这里,关于Unity调用系统选择文件功能实现文件的选择及另存为的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!