博主原创!
最近,要开发一款音乐播放软件。用unity开发,现在用到了一个功能,需要有一个按钮功能,打开windows文件夹,并选取所需要的视频,交给videoplayer进行播放。而百度上所搜到的答案,都大相近庭,更准确来说代码都是一样的,功能都是一样的,不信你可以搜索看看。大部分比较准确的答案都是切换图片,但是代码太片段话了,所以我自己整理换成我自己想要的功能。
第一个脚本,OpenFileName.cs;
//调用系统的窗口
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 WindowDll
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOpenFileName1([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
}
第二个脚本,OpenFile.cs;
//此脚本是封装在一个类中,想要使用的话可以,可以在unity界面放到按钮的Onclick()上;
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
using UnityEngine.Video;
using UnityEngine.UI;
public class OpenFile : MonoBehaviour
{
public Button button;
public void OpenFileWin()
{
GameObject.FindWithTag ("moviePlayer").GetComponent<VideoPlayer> ().Pause ();
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "All Files\0*.*\0\0";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
string path = Application.streamingAssetsPath;
path = path.Replace('/','\\');
//默认路径
ofn.initialDir = path;
//ofn.initialDir = "D:\\MyProject\\UnityOpenCV\\Assets\\StreamingAssets";
ofn.title = "Open Project";
ofn.defExt = "JPG";//显示文件的类型
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
if (WindowDll.GetOpenFileName(ofn))
{
Debug.Log("Selected file with full path: {0}" + ofn.file);
}
//此处更改了大部分答案的协程方法,在这里是采用unity的videoplayer.url方法播放视频;
//
/*而且我认为大部分的其他答案,所给的代码并不全,所以,想要其他功能的人,可以仿照下面的代码,直接在此类中写功能。
//*/
GameObject.FindWithTag ("moviePlayer").GetComponent<VideoPlayer> ().url = ofn.file;
GameObject.FindWithTag ("moviePlayer").GetComponent<VideoPlayer> ().Play ();
}
}
按钮功能
但是,次方法有一个bug,笔者还在调试研究。当点击按钮调出file文件夹的时候,再次点击按钮会造成程序崩溃。
2017.10.12 改
bug找到解决办法,打包出来运行之后,这个问题就不存在了。问题的原因可能是选择文件的时候并不是unity自己的根目录,出现的错误提示。