①找到System.Windows.Forms.dll:在unity的安装目录中找到它,如 E:\ProgramFiles(x86)\Unity\Editor\Data\Mono\lib\mono\2.0
②设置.NET 2.0集:Untiy默认是.NET 2.0 Subset。在Edit->Project Settings->Player->OtherSettings中修改
③任意打开一项目,新建Plugins文件夹,将找到的System.Windows.Forms.dll复制进去工程文件中(Project)
//测试:不修改.NET 2.0程序集不打包能够在编辑器中运行
//打包exe会报错
using UnityEngine;
using System.Collections;
using System;
using System.Windows.Forms;
using System.IO;
using UnityEngine.UI;
public class AddHead : MonoBehaviour {
private Texture2D img=null;
public Image image;
public Sprite sprite;
public void addhead(){
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));
}
image.sprite=sprite;
}
IEnumerator GetTexture(string url){
WWW www = new WWW (url);
yield return www;
if (www.isDone && www.error == null) {
img=www.texture;
sprite=Sprite.Create(img,new Rect(0,0,img.width,img.height),new Vector2(0.5f,0.5f));
image.sprite=sprite;
//Debug.Log(img.width+" "+img.height);
byte[] date=img.EncodeToPNG();
}
}
}