如何用Unity3D实现选择并加载本地图片的功能?

2026-04-18 23:192阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计465个文字,预计阅读时间需要2分钟。

如何用Unity3D实现选择并加载本地图片的功能?

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

找到System.Windows.Forms.dll:

如何用Unity3D实现选择并加载本地图片的功能?

在Unity的安装目录中找到它,例如:E:\Program Files (x86)\Unity\Editor\Data\Mono

结果:在Unity的安装目录中找到System.Windows.Forms.dll,路径为E:\Program Files (x86)\Unity\Editor\Data\Mono

本文实例为大家分享了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()的方法

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

看运行效果

——》——》

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

本文共计465个文字,预计阅读时间需要2分钟。

如何用Unity3D实现选择并加载本地图片的功能?

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

找到System.Windows.Forms.dll:

如何用Unity3D实现选择并加载本地图片的功能?

在Unity的安装目录中找到它,例如:E:\Program Files (x86)\Unity\Editor\Data\Mono

结果:在Unity的安装目录中找到System.Windows.Forms.dll,路径为E:\Program Files (x86)\Unity\Editor\Data\Mono

本文实例为大家分享了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()的方法

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

看运行效果

——》——》

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