Unity調用Windows對話框保存文件

       最近有個PC上的項目,其中有個功能是將項目內的數據保存到電腦上,就像平常我們在windows下將文件另存到其他地方一樣,話不多說上代碼

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

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]  

public class OpenFileData 
{
    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;
}


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

       然後是測試代碼

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

public class SaveTest : MonoBehaviour {

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 35), "SaveTxtBtn"))
        {
            OpenFileData ofn = new OpenFileData();
            ofn.structSize = Marshal.SizeOf(ofn);
            ofn.filter = "txt (*.txt)";//是什麼文件類型就修改此處
            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;//默認路徑
            ofn.title = "Save Txt";
            ofn.defExt = "txt";//是什麼文件類型就修改此處
            ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
            if (SaveDll.GetSaveFileName(ofn))
            {
                StartCoroutine(WaitSaveTxt(ofn.file));
            }
        }


        if (GUI.Button(new Rect(0, 100, 100, 35), "SaveExcelBtn"))
        {
            OpenFileData ofn = new OpenFileData();
            ofn.structSize = Marshal.SizeOf(ofn);
            ofn.filter = "csv (*.csv)";
            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;//默認路徑
            ofn.title = "Save Excel";
            ofn.defExt = "csv";
            ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
            if (SaveDll.GetSaveFileName(ofn))
            {
                StartCoroutine(WaitSaveExcel(ofn.file));
            }
        }
    }

    IEnumerator WaitSaveTxt(string fileName)
    {
        FileInfo fi = new FileInfo(UnityEngine.Application.dataPath + "/Resources/TxtTest.txt");
        fi.CopyTo(fileName, true);
        yield return fi;

    }

    IEnumerator WaitSaveExcel(string fileName)
    {
        FileInfo fi = new FileInfo(UnityEngine.Application.dataPath + "/Resources/test.csv");        
        fi.CopyTo(fileName, true);
        yield return fi;
    }

}

       試了下Excel和Txt類型的文件都可以另存到windows上自己選擇的位置,理論上可以存任意類型的文件,算是解決了項目的需要。

       項目內沒有加入任何額外插件,示例工程地址:https://download.csdn.net/download/abc1090275833/10983204

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章