【源码】Unity3D快速调用windows文件选择对话框
1、1. 打来Unity3D软件,创建工程如下图所示。

2、2. 在【Project】面板下空白区域鼠标右键->【Create】->【Folder】,创建一个文件夹【Plugins】如下图所示。

3、3. 打开Plugins文件夹所在的位置,添加【System..Forms.dll】文件如下图所示。下载链接: https://pan.baidu.com/s/1JKVkYrSlv-cBga5P1all7g 密码: ihfw

4、4. 创建下图所示的脚本文件,并将其拖拽到MainCamera对象下,如图所示

5、5. 打开步骤四创建的脚本文件,编辑代码如下图所示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testOpenFileDialog : MonoBehaviour {
// Use this for initialization
void Update()
{
if (Input.GetKeyDown(KeyCode.O))
{
string s = loadFile();
if (s != "")
testSuccess(s);
}
}
void testSuccess(string path)
{
Debug.Log("打开文件路径: " + path);
}
public static string loadFile()
{
string bgImagePath = "";
//加载图片的对话框,是在编辑模式下的
string extion = "png,jpg";
string path = "";
#if UNITY_EDITOR
// Editor specific code here
path = UnityEditor.EditorUtility.OpenFilePanel("Load Images of Directory", UnityEngine.Application.dataPath, extion);
#endif
//WWW ww = new WWW("file:///" + path);
//print(ww.url);
if (path != "")//load image as texture
{
//StartCoroutine(WaitLoad(path));
Debug.Log("获得文件路径成功:" + path);
bgImagePath = path;
}
return bgImagePath;
}
}

6、6. 运行软件,在Game视图按下键盘O键,弹出加载文件对话框。

7、7. 运行控制台输出打开文件的路径信息,工程源码见注意事项。

8、如果您觉得有用,记得在下方点击投票、点赞、关注、留言,小编会定期奉上更多的惊喜哦,您的支持才是小编继续努力的动力,么么哒。
