1.找到System.Windows.Forms.dll,并放在project中的Plugins文件夹下。 “D:\Unity3D\Editor\Data\Mono\lib\mono\2.0\System.Windows.Forms.dll”
2.打开Player Settings 把.NET 2.0 Subset 改为.NET 2.0;
3.选择的图片路径中不能存在中文
using UnityEngine;
using System.Collections;
using System;
using System.Windows.Forms;
using System.IO;
public class ReadPic : 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("file://"+od.FileName));
}
}
if (img != null)
{
GUI.DrawTexture(new Rect(0, 20, img.width, img.height), img);
}
}
IEnumerator GetTexture(string url)
{
WWW www = new WWW(url);
yield return www;
if (www.isDone && www.error == null)
{
img = www.texture;
Debug.Log(img.width + " " + img.height);
byte[] data = img.EncodeToPNG();
// File.WriteAllBytes(UnityEngine.Application.streamingAssetsPath + "/Temp/temp.png", data);
}
}
}
</pre><p></p><pre code_snippet_id="515231" snippet_file_name="blog_20141111_2_8320733" name="code" class="csharp">