当前位置: 移动技术网 > IT编程>开发语言>c# > Unity3D选择本地图片并加载

Unity3D选择本地图片并加载

2019年09月08日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了unity3d选择本地图片并加载的具体代码,供大家参考,具体内容如下

①找到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)

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;
// use this for initialization
 void start () {
 }
 // update is called once per frame
 void update () {
 }
 /*void ongui(){
 if (gui.button (new rect (0, 0, 100, 20), "选择文件")) {
 }*/
 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));
 }
 /*if (img != null) {
 //gui.drawtexture(new rect(0,20,img.width,img.height),img);
 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();
 }
 }
}

为按钮绑定一个addhead()的方法

在编辑器运行,当出现此弹窗,确定忽略即可。发布成桌面客户端是不会有这个弹窗的。

看运行效果

——》——》

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网