unity----OpenFileDialog打开选择对话框

unity----OpenFileDialog打开选择对话框

原创  2015年02月10日 12:10:11
  • 3755

 

[csharp]  view plain  copy
 
  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 DllTest  
  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.     {  
  42.         return GetOpenFileName(ofn);  
  43.     }  
  44. }  

[csharp]  view plain  copy
 
  1. using UnityEngine;  
  2.   
  3. using System.Collections;  
  4.   
  5. using System.Text;  
  6.   
  7. using System.Runtime.InteropServices;  
  8.   
  9. using System;  
  10.   
  11. public class Test : MonoBehaviour {  
  12.   
  13.     public GameObject plane;  
  14.   
  15.     void OnGUI()  
  16.     {  
  17.         if(GUI.Button(new Rect(0,0,100,35),"OpenDialog"))  
  18.         {  
  19.             OpenFileName ofn = new OpenFileName();  
  20.   
  21.             ofn.structSize = Marshal.SizeOf(ofn);  
  22.   
  23.             //三菱(*.gxw)\0*.gxw\0西门子(*.mwp)\0*.mwp\0All Files\0*.*\0\0  
  24.             ofn.filter = "All Files\0*.*\0\0";  
  25.   
  26.             ofn.file = new string(new char[256]);  
  27.   
  28.             ofn.maxFile = ofn.file.Length;  
  29.   
  30.             ofn.fileTitle = new string(new char[64]);  
  31.   
  32.             ofn.maxFileTitle = ofn.fileTitle.Length;  
  33.   
  34.             ofn.initialDir =UnityEngine.Application.dataPath;//默认路径  
  35.   
  36.             ofn.title = "Open Project";  
  37.   
  38.             ofn.defExt = "JPG";//显示文件的类型  
  39.            //注意 一下项目不一定要全选 但是0x00000008项不要缺少  
  40.             ofn.flags=0x00080000|0x00001000|0x00000800|0x00000200|0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR  
  41.   
  42.             if(DllTest.GetOpenFileName( ofn ))  
  43.             {  
  44.   
  45.                // StartCoroutine(WaitLoad(ofn.file));//加载图片到panle  
  46.   
  47.                 Debug.Log( "Selected file with full path: {0}"+ofn.file );  
  48.   
  49.             }  
  50.   
  51.         }  
  52.   
  53.     }  
  54.   
  55.     IEnumerator WaitLoad(string fileName)  
  56.     {  
  57.         WWW wwwTexture=new WWW("file://"+fileName);  
  58.   
  59.         Debug.Log(wwwTexture.url);  
  60.   
  61.         yield return wwwTexture;  
  62.   
  63.         plane.renderer.material.mainTexture=wwwTexture.texture;  
  64.     }  
  65. }  

猜你喜欢

转载自hypercube.iteye.com/blog/2399620