天天看點

Unity 3D調用Windows打開、儲存視窗、檔案浏覽器12支援

Unity調用Window視窗

本文提供全流程,中文翻譯。

Chinar 堅持将簡單的生活方式,帶給世人!

(擁有更好的閱讀體驗 —— 高分辨率使用者請根據需求調整網頁縮放比例)

Chinar —— 心分享、心創新!

助力快速完成 Unity調用 Window api 執行儲存/打開操作

為新手節省寶貴的時間,避免采坑!

Chinar 教程效果:

全文高清圖檔,點選即可放大觀看 (很多人竟然不知道)

用來控制打開/儲存項目檔案

将兩個腳本放到項目中,分别綁定2個按鈕 打開/儲存

ChinarFileController 腳本需要挂載到空物體上

即可正常調用

具體需求,需要自己來定,儲存到某個路徑下

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


/// <summary>
/// 檔案控制腳本
/// </summary>
public class ChinarFileController : MonoBehaviour
{
    /// <summary>
    /// 打開項目
    /// </summary>
    public void OpenProject()
    {
        OpenFileDlg pth  = new OpenFileDlg();
        pth.structSize   = Marshal.SizeOf(pth);
        pth.filter       = "All files (*.*)|*.*";
        pth.file         = new string(new char[256]);
        pth.maxFile      = pth.file.Length;
        pth.fileTitle    = new string(new char[64]);
        pth.maxFileTitle = pth.fileTitle.Length;
        pth.initialDir   = Application.dataPath.Replace("/", "\\") + "\\Resources"; //預設路徑
        pth.title        = "打開項目";
        pth.defExt       = "dat";
        pth.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetOpenFileName(pth))
        {
            string filepath = pth.file; //選擇的檔案路徑;  
            Debug.Log(filepath);
        }
    }


    /// <summary>
    /// 儲存檔案項目
    /// </summary>
    public void SaveProject()
    {
        SaveFileDlg pth  = new SaveFileDlg();
        pth.structSize   = Marshal.SizeOf(pth);
        pth.filter       = "All files (*.*)|*.*";
        pth.file         = new string(new char[256]);
        pth.maxFile      = pth.file.Length;
        pth.fileTitle    = new string(new char[64]);
        pth.maxFileTitle = pth.fileTitle.Length;
        pth.initialDir   = Application.dataPath; //預設路徑
        pth.title        = "儲存項目";
        pth.defExt       = "dat";
        pth.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (SaveFileDialog.GetSaveFileName(pth))
        {
            string filepath = pth.file; //選擇的檔案路徑;  
            Debug.Log(filepath);
        }
    }
}           

無需挂載到空物體上

using System.Runtime.InteropServices;
using System;


/// <summary>
/// 檔案日志類
/// </summary>
// [特性(布局種類.有序,字元集=字元集.自動)]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class ChinarFileDlog
{
    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;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg : ChinarFileDlog
{
}

public class OpenFileDialog
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
}

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

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SaveFileDlg : ChinarFileDlog
{
}           

運作效果:

擁有自己的伺服器,無需再找攻略!

Chinar 提供一站式教程,閉眼式建立!

為新手節省寶貴時間,避免采坑!

先點選領取 —— 阿裡全産品優惠券 (享受最低優惠)

1 ——

雲伺服器超全購買流程 (新手必備!)

2 ——

阿裡ECS雲伺服器自定義配置 - 購買教程(新手必備!)

3——

Windows 伺服器配置、運作、建站一條龍 !

4 ——

Linux 伺服器配置、運作、建站一條龍 !

Chinar

END

本部落格為非營利性個人原創,除部分有明确署名的作品外,所刊登的所有作品的著作權均為本人所擁有,本人保留所有法定權利。違者必究

對于需要複制、轉載、連結和傳播部落格文章或内容的,請及時和本部落客進行聯系,留言,Email: [email protected]

對于經本部落客明确授權和許可使用文章及内容的,使用時請注明文章或内容出處并注明網址

繼續閱讀