Unity3D选择本地图片并加载
本文实例为大家分享了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()的方法
在编辑器运行,当出现此弹窗,确定忽略即可。发布成桌面客户端是不会有这个弹窗的。
看运行效果
——》
——》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)