二、编写对应的控制脚本
/***
* Title:"智慧工厂" 项目
* 主题:打开文件
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
namespace kernal
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenSelectFileName
{
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;
}//Class_end
}
/***
* Title:"智慧工厂" 项目
* 主题:本地弹窗
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Runtime.InteropServices;
namespace Kernal
{
public class LocalDialog
{
//链接指定系统函数 打开文件对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenSelectFileName ofn);
public static bool GetOFN([In, Out] OpenSelectFileName ofn)
{
return GetOpenFileName(ofn);
}
//链接指定系统函数 另存为对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenSelectFileName ofn);
public static bool GetSFN([In, Out] OpenSelectFileName ofn)
{
return GetSaveFileName(ofn);
}
}//Class_end
}
/***
* Title:"智慧工厂" 项目
* 主题:选择资源或者路径方法
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace kernal
{
public class SelectFileOrPath
{
private static SelectFileOrPath _Instance; //本类实例
#region 属性
private string _SelectFilePath; //选择的文件路径
private string _SelectFile; //选择文件
private string _FileName; //文件名称
//选择文件路径
public string SelectFilePath
{
get
{
return _SelectFilePath;
}
set
{
_SelectFilePath = value;
}
}
//选择文件
public string SelectFile
{
get
{
return _SelectFile;
}
set
{
_SelectFile = value;
}
}
//文件名称
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
#endregion
/// <summary>
/// 本类实例
/// </summary>
/// <returns></returns>
public static SelectFileOrPath GetInstance()
{
if (_Instance==null)
{
_Instance = new SelectFileOrPath();
}
return _Instance;
}
/// <summary>
/// 选择文件路径(Unity自带的方法)
/// </summary>
public void SelectFolderPath_Unity()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
SelectFilePath = fbd.SelectedPath;
}
}
/// <summary>
/// 选择文件(Unity自带的方法)
/// </summary>
public void SelectFileSelf_Unity()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "file://" + UnityEngine.Application.dataPath;//默认打开路径
if (ofd.ShowDialog() == DialogResult.OK)
{
SelectFile = ofd.FileName;
}
}
/// <summary>
/// 选择文件路径(调用Windows方法)
/// </summary>
public void SelectFilePath_Windows()
{
OpenSelectFileName openFileName = new OpenSelectFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "PDF文件(*.PDF)\0*.PDF|数据库文件(*.bak)\0*.bak|*.*\0*.*";
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 = UnityEngine.Application.streamingAssetsPath.Replace('/', '\\');//默认路径
openFileName.title = "请选择路径或者文件";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (LocalDialog.GetSaveFileName(openFileName))
{
SelectFilePath = openFileName.file;
FileName= openFileName.file;
}
}
}//Class_end
}
三、测试方法
/***
* Title:"智慧工厂" 项目
* 主题:测试选择路径或文件
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Control;
using kernal;
namespace SimpleUIFrame
{
public class Test_BackUpSqlServerDatabase : MonoBehaviour
{
private string _BackUpPath; //备份路径
void Start()
{
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
bool isSuccess = false;
//选择备份路径
SelectFileOrPath.GetInstance().SelectFilePath_Windows();
_BackUpPath = SelectFileOrPath.GetInstance().SelectFilePath;
if (!string.IsNullOrEmpty(_BackUpPath))
{
Debug.Log("备份文件的路径=" + _BackUpPath);
}
}//Class_end
}
四、效果图如下
![]()