目录
背景:
项目中需要在Windows端实现保存文件,这个时候需要调用Windows给的保存对话窗口获取一个保存路径用来写入文件
过程:
网上找了一番后参考了这个代码实现了功能
unity调用windows窗口选择文件并保存_u3d选择文件-CSDN博客
public void OpenSelectsSGFFile(Action<string> CallBackSuccess, Action CallBackFail = null)
{
try
{
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "棋谱文件(*.sgf)\0*.sgf";
openFileName.file = new string(new char[256]);
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new string(new char[64]);
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.title = "窗口标题";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008 | 0x00004000;
openFileName.defExt = "sgf";
openFileName.initialDir = PlayerPrefs.GetString("LastChessNamePath", "C:\\");
openFileName.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶
if (GetSaveFileName(openFileName))
{
string filepath = openFileName.file;
string filepathDir = RemoveEnd(filepath, openFileName.fileTitle);
PlayerPrefs.SetString("LastChessNamePath", filepathDir);
CallBackSuccess?.Invoke(filepath);
}
else
{
CallBackFail?.Invoke();
}
}
catch (Exception e)
{
Debug.Log("系统繁忙,请重试");
}
}
到这里还没什么问题,功能能够正常实现,测试那边也没有问题。
后来需求又来了,希望能够打开保存对话窗口的时候能够有一个默认文件名字。
那么就修改一下代码,添加了一个参数
public void OpenSelectsSGFFile(string FileName, Action<string> CallBackSuccess, Action CallBackFail = null)
{
try
{
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "棋谱文件(*.sgf)\0*.sgf";
openFileName.file = FileName;
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = FileName;
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.title = "窗口标题";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008 | 0x00004000;
openFileName.defExt = "sgf";
openFileName.initialDir = PlayerPrefs.GetString("LastChessNamePath", "C:\\");
openFileName.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶
if (GetSaveFileName(openFileName))
{
string filepath = openFileName.file;
string filepathDir = RemoveEnd(filepath, openFileName.fileTitle);
PlayerPrefs.SetString("LastChessNamePath", filepathDir);
CallBackSuccess?.Invoke(filepath);
}
else
{
CallBackFail?.Invoke();
}
}
catch (Exception e)
{
Debug.Log("系统繁忙,请重试");
}
}
运行了一下,功能正常便直接交给测试了。
问题:
然后问题来了,测试那边反馈,调用这个保存框多几次后程序会发生崩溃
尝试了几次定位到了是【openFileName.file = FileName;】这里有问题,原本怀疑是【openFileName.maxFile】 给的太小,后面给了256,512,10000依旧会有闪退。
观察到【openFileName.file = new string(new char[256]);】
这里面new了一个新的string不会闪退,怀疑是不是c++里面引用到了一个空指针对象导致的崩溃,然后尝试了这种代码
【openFileName.file = new string(FileName.ToCharArray());】
结果还是不行
折磨啊,折腾了好一会后,我搞出了这种代码
char[] c = new char[256];
openFileName.file = new string(c);
这种代码也不会崩溃................斯~,他该不会和string长度有关吧。
解决方案:
public void OpenSelectsSGFFile(string FileName, Action<string> CallBackSuccess, Action CallBackFail = null)
{
try
{
string name= CopyNewString(FileName);
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "棋谱文件(*.sgf)\0*.sgf";
openFileName.file = name;//文件原始路径,格式(D:\3-图片-花.jpg)
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = name;//文件名字(带格式后缀),格式(3-图片-花.jpg)
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.title = "窗口标题";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008 | 0x00004000;
openFileName.defExt = "sgf";
openFileName.initialDir = PlayerPrefs.GetString("LastChessNamePath", "C:\\");
openFileName.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶
if (GetSaveFileName(openFileName))
{
string filepath = openFileName.file;
string filepathDir = RemoveEnd(filepath, openFileName.fileTitle);
PlayerPrefs.SetString("LastChessNamePath", filepathDir);
CallBackSuccess?.Invoke(filepath);
}
else
{
CallBackFail?.Invoke();
}
}
catch (Exception e)
{
Debug.Log("系统繁忙,请重试");
}
}
private string CopyNewString(string str)
{
int max = 2048;
int len = 256;
while(len < str.Length && len < max)
{
len = len * 2;
}
char[] c = new char[len];
char[] def = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
c[i] = def[i];
}
return new string(c);
}
弄了一个new char[256] 存文件名字符串,您猜怎么着?好了。
打包给测试验收,通过。
狗屎代码
代码全文
using System;
using System.Runtime.InteropServices;
using UnityEngine;
[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 = new string(new char[1024]);
public int maxFile = 0;
public String fileTitle = new string(new char[1024]);
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;
}
public class FolderBrowserHelperByWin : SingletonBase<FolderBrowserHelperByWin>
{
#region Window
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
#endregion
private String RemoveEnd(String filepath, string fileName)
{
filepath = filepath.Trim();
Debug.Log(filepath.Replace(fileName, "").Trim());
return filepath.Replace(fileName, "").Trim();
}
public void OpenSelectsSGFFile(string FileName, Action<string> CallBackSuccess, Action CallBackFail = null)
{
try
{
string name= CopyNewString(FileName);
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "棋谱文件(*.sgf)\0*.sgf";
openFileName.file = name;//文件原始路径,格式(D:\3-图片-花.jpg)
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = name;//文件名字(带格式后缀),格式(3-图片-花.jpg)
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.title = "窗口标题";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008 | 0x00004000;
openFileName.defExt = "sgf";
openFileName.initialDir = PlayerPrefs.GetString("LastChessNamePath", "C:\\");
openFileName.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶
if (GetSaveFileName(openFileName))
{
string filepath = openFileName.file;
string filepathDir = RemoveEnd(filepath, openFileName.fileTitle);
PlayerPrefs.SetString("LastChessNamePath", filepathDir);
CallBackSuccess?.Invoke(filepath);
}
else
{
CallBackFail?.Invoke();
}
}
catch (Exception e)
{
Debug.Log("系统繁忙,请重试");
}
}
private string CopyNewString(string str)
{
int max = 2048;
int len = 256;
while(len < str.Length && len < max)
{
len = len * 2;
}
char[] c = new char[len];
char[] def = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
c[i] = def[i];
}
return new string(c);
}
}