首页 > 技术文章 > GJM : Unity调用系统窗口选择本地文件

TDou 2016-12-09 16:27 原文

 

  • 感谢您的阅读。喜欢的、有用的就请大哥大嫂们高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力以及转载收藏动力。欢迎转载!
  • 版权声明:本文原创发表于 【请点击连接前往】 ,未经作者同意必须保留此段声明!如有问题请联系我,侵立删,谢谢!
  • 我的博客:http://www.cnblogs.com/GJM6/  -  传送门:【点击前往

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4. using System.Runtime.InteropServices;  
  5.   
  6. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  7.   
  8. public class OpenFileName  
  9. {  
  10.     public int structSize = 0;  
  11.     public IntPtr dlgOwner = IntPtr.Zero;  
  12.     public IntPtr instance = IntPtr.Zero;  
  13.     public String filter = null;  
  14.     public String customFilter = null;  
  15.     public int maxCustFilter = 0;  
  16.     public int filterIndex = 0;  
  17.     public String file = null;  
  18.     public int maxFile = 0;  
  19.     public String fileTitle = null;  
  20.     public int maxFileTitle = 0;  
  21.     public String initialDir = null;  
  22.     public String title = null;  
  23.     public int flags = 0;  
  24.     public short fileOffset = 0;  
  25.     public short fileExtension = 0;  
  26.     public String defExt = null;  
  27.     public IntPtr custData = IntPtr.Zero;  
  28.     public IntPtr hook = IntPtr.Zero;  
  29.     public String templateName = null;  
  30.     public IntPtr reservedPtr = IntPtr.Zero;  
  31.     public int reservedInt = 0;  
  32.     public int flagsEx = 0;  
  33. }  
  34.    
  35. public class WindowDll  
  36. {  
  37.     [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]  
  38.     public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);  
  39.     public static bool GetOpenFileName1([In, Out] OpenFileName ofn)  
  40.     {  
  41.         return GetOpenFileName(ofn);  
  42.     }  
  43. }  

 

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Runtime.InteropServices;  
  4. using System.Reflection;  
  5.   
  6. public class CameraTest : MonoBehaviour  
  7. {  
  8.     public WebCamTexture cameraTexture;  
  9.     public string cameraName = "";  
  10.     private bool isPlay = false;  
  11.     // Use this for initialization  
  12.     void Start()  
  13.     {  
  14.         //StartCoroutine(OpenCamera());  
  15.     }  
  16.   
  17.     // Update is called once per frame  
  18.     void Update()  
  19.     {  
  20.   
  21.     }  
  22.     /// <summary>  
  23.     /// 获取权限打开摄像头  
  24.     /// </summary>  
  25.     /// <returns></returns>  
  26.     IEnumerator OpenCamera()  
  27.     {  
  28.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
  29.         if (Application.HasUserAuthorization(UserAuthorization.WebCam))  
  30.         {  
  31.             WebCamDevice[] devices = WebCamTexture.devices;  
  32.             cameraName = devices[0].name;  
  33.             cameraTexture = new WebCamTexture(cameraName, 320, 240, 15);  
  34.             cameraTexture.Play();  
  35.             isPlay = true;  
  36.         }  
  37.     }  
  38.   
  39.     void OnGUI()  
  40.     {  
  41.         if (isPlay)  
  42.         {  
  43.             GUI.DrawTexture(new Rect(0, 0, 320, 240), cameraTexture, ScaleMode.ScaleAndCrop);  
  44.         }  
  45.   
  46.         if (GUI.Button(new Rect(0, 0, 100, 35), "OpenDialog"))  
  47.         {  
  48.             OpenFileName ofn = new OpenFileName();  
  49.   
  50.             ofn.structSize = Marshal.SizeOf(ofn);  
  51.   
  52.             ofn.filter = "All Files\0*.*\0\0";  
  53.   
  54.             ofn.file = new string(new char[256]);  
  55.   
  56.             ofn.maxFile = ofn.file.Length;  
  57.   
  58.             ofn.fileTitle = new string(new char[64]);  
  59.   
  60.             ofn.maxFileTitle = ofn.fileTitle.Length;  
  61.             string path = Application.streamingAssetsPath;  
  62.             path = path.Replace('/','\\');  
  63.             //默认路径  
  64.             ofn.InitialDirectory = path;  
  65.             //ofn.InitialDirectory = "D:\\MyProject\\UnityOpenCV\\Assets\\StreamingAssets";  
  66.             ofn.title = "Open Project";  
  67.   
  68.             ofn.defExt = "JPG";//显示文件的类型  
  69.             //注意 一下项目不一定要全选 但是0x00000008项不要缺少  
  70.             ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR  
  71.   
  72.             if (WindowDll.GetOpenFileName(ofn))  
  73.             {  
  74.                 Debug.Log("Selected file with full path: {0}" + ofn.file);  
  75.             }  
  76.   
  77.         }  
  78.     }  
  79. }  



 


 

原文地址点击这里

文件的类型设置

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. ofn.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";  

推荐阅读