网上有两个常用的方法,我先用了第一种。
第一种方法:
需要注意三点,首先导入system.window.Forms.dll库,导进去后一定记得要修改playersetting下的optimization->API compatibility level为.NET 2.0,至于为什么改成这样,可以参考unity3d 的解释:http://game.ceeger.com/Manual/MobileDotnet.html。
还要注意一点:加载的本地图片地址一定不能带有中文路径
另外,www新建的时候要加上“file://” + url;
WWW www = new WWW("file://" + url); //注意要加上"file://"
只需要一个脚本,其中代码如下:
using UnityEngine;
using System.Collections;
using System.Windows.Forms;
using System.IO;
public class UploadImg: MonoBehaviour {
public Texture2D img = null;
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 20), "选择文件"))
{
OpenFileDialog od = new OpenFileDialog();
od.Title = "请选择头像图片";
od.Multiselect = false;
od.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
if (od.ShowDialog() == DialogResult.OK)
{
Debug.Log(od.FileName);
StartCoroutine(GetTexture(od.FileName));
}
}
if (img != null)
{
GUI.DrawTexture(new Rect(0, 20, img.width/2, img.height/2), img);
}
}
IEnumerator GetTexture(string url)
{
WWW www = new WWW("file://" + url); //注意要加上"file://"
yield return www;
if (www.isDone)
{
img = www.texture;
}
}
}
效果是这样的,但是感觉和win7风格差的太远
第二种方法看起来要炫多了:
需要两个脚本。
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);
}
}
UploadImage.cs如下:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class Upload : MonoBehaviour {
public Texture2D img = null;
private bool click = false;
void OpenDialog()
{
click = true;
}
void EnterImage()
{
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;
ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
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))
{
StartCoroutine(GetTexture(ofn.file));
}
}
IEnumerator GetTexture(string url)
{
WWW wwwTexture = new WWW ("file://" + url);
yield return wwwTexture;
if(wwwTexture.isDone)
img = wwwTexture.texture;
}
void OnGUI()
{
if(click)
{
EnterImage();
click = false;
}
if (img != null)
GUI.DrawTexture (new Rect(Screen.width/2 - img.width/2,Screen.height/2 - img.height,img.width,img.height),img);
}
}