多语言展示
当前在线:1728今日阅读:155今日分享:35

Unity 实用教程 之 调用系统窗口选择文件或路径

Unity 实用教程 之 调用系统窗口选择文件或路径。本节介绍,在Unity开发中,调用系统窗口进行文件或路径选择,具体如下
工具/原料

Unity

方法/步骤
1

打开Unity,新建一个空文件,具体如下图

2

在工程中,新建两个脚本,OpenFileName 和 DialogTest,双击脚本或者 右键 Open C# Project,具体如下图

3

OpenFileName 脚本具体代码和简单代码解释如下图

4

OpenFileName 具体内容如下:using UnityEngine;using System.Collections;using System;using System.Runtime.InteropServices;[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;}public class LocalDialog{    //链接指定系统函数       打开文件对话框    [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);    }}

5

DialogTest 具体代码和简单代码解释如下图

6

DialogTest 的具体内容如下:using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class DialogTest : MonoBehaviour{    void OnGUI()    {        if (GUI.Button(new Rect(10, 10, 100, 50), 'Open'))        {            OpenFileName openFileName = new OpenFileName();            openFileName.structSize = Marshal.SizeOf(openFileName);            openFileName.filter = 'Excel文件(*.xlsx)\0*.xlsx';            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.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径            openFileName.title = '窗口标题';            openFileName.flags = 0x | 0x | 0x | 0x;            if (LocalDialog.GetOpenFileName(openFileName))            {                Debug.Log(openFileName.file);                Debug.Log(openFileName.fileTitle);            }        }    }}

7

脚本编译正确,回到Unity界面,把脚本 DialogTest 挂在到 MainCamera,具体如下图

8

运行场景,按下游戏窗口的 Open ,就会出现系统文件窗口,具体如下图

9

找个Excel文件,点击 保存 ,控制台 Console 打印如下图

10

到此,《Unity 实用教程 之 调用系统窗口选择文件或路径》讲解结束,谢谢

注意事项

您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

推荐信息