问题描述:
有个地方需要用户上传一张图片,网上查了,使用了如下代码:
string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");
然后在打包发布时遇到问题,因为EditorUtility使用了UnityEditor,无法发布,无奈只能另寻解决方法
解决方案:
新建一个脚本文件将下面的代码粘进去,静态加载即可
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;/// <summary>
/// 调用系统代码,打开本地文件夹
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenDialogueFile
{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 OpenDialogDir
{public IntPtr hwndOwner = IntPtr.Zero;public IntPtr pidlRoot = IntPtr.Zero;public String pszDisplayName = null;public String lpszTitle = null;public UInt32 ulFlags = 0;public IntPtr lpfn = IntPtr.Zero;public IntPtr lParam = IntPtr.Zero;public int iImage = 0;
}[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 static class FolderBrowserHelper
{#region Window[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetOpenFileName([In, Out] OpenDialogueFile ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetSaveFileName([In, Out] OpenDialogueFile ofn);[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);#endregionpublic const string IMAGEFILTER = "图片文件(*.jpg;*.png)\0*.jpg;*.png";public const string ALLFILTER = "所有文件(*.*)\0*.*";/// <summary>/// 选择文件/// </summary>/// <param name="callback">返回选择文件夹的路径</param>/// <param name="filter">文件类型筛选器</param>public static string SelectFile(string filter = ALLFILTER){try{OpenFileName openFileName = new OpenFileName();openFileName.structSize = Marshal.SizeOf(openFileName);// openFileName.initialDir = "初始目录"openFileName.filter = filter;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.title = "选择文件";openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;if (GetSaveFileName(openFileName)){string filepath = openFileName.file; //选择的文件路径; if (File.Exists(filepath)){return filepath;}}}catch (Exception e){Debug.LogError(e);}return null;}}
调用方法如下:
string imagePath = FolderBrowserHelper.SelectFile();
这样可以正常替代原本的
string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");