本文实例为大家分享了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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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()的方法
在编辑器运行,当出现此弹窗,确定忽略即可。发布成桌面客户端是不会有这个弹窗的。
看运行效果
——》——》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u014725878/article/details/50070697