溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)Unity C# GetSaveFileName()

發(fā)布時(shí)間:2020-07-14 16:58:13 來(lái)源:億速云 閱讀:395 作者:Leah 欄目:編程語(yǔ)言

如何實(shí)現(xiàn)Unity C# GetSaveFileName()?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

先放個(gè)效果圖:

如何實(shí)現(xiàn)Unity C# GetSaveFileName()

首先需要定義一個(gè)OpenFileName的類:

[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;
}

  當(dāng)然,也不是都用到了,只用到了一小部分,有性趣的童鞋可以個(gè)性化一下保存對(duì)話框,

然后是最重要的委托GetSaveName()方法:

public class DllUse
{
	[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
	public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
}

  擋擋擋,最后調(diào)用一下即可:

public static void OpenDialog(Action<Stream> onSave)
    {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = Marshal.SizeOf(ofn);
    
        ofn.filter = "Excel (*.xls)\0*.xls\0\0";
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = UnityEngine.Application.dataPath;//默認(rèn)路徑ofn.title = "保存文件";
        ofn.defExt = ".xls";//顯示文件的類型ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (DllTest.GetSaveFileName(ofn))
        {
            dosomething();string Savepath = Path.GetDirectoryName (ofn.file);
            Process.Start (Savepath);
        }
    }

如何實(shí)現(xiàn)Unity C# GetSaveFileName()

如何實(shí)現(xiàn)Unity C# GetSaveFileName()

看完上述內(nèi)容,你們掌握如何實(shí)現(xiàn)Unity C# GetSaveFileName()的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI