Unity3D选择本地图片并加载
作者:测试猫 时间:2023-11-20 09:25:05
本文实例为大家分享了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()的方法
在编辑器运行,当出现此弹窗,确定忽略即可。发布成桌面客户端是不会有这个弹窗的。
看运行效果
——》
——》
来源:https://blog.csdn.net/u014725878/article/details/50070697
标签:Unity3D,图片加载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Java实现的Dijkstra算法示例
2021-09-17 02:51:13
Spring MVC 使用支付宝接口完成在线支付的示例代码
2023-11-29 04:07:55
![](https://img.aspxhome.com/file/2023/1/60321_0s.png)
一文搞懂Java中的序列化与反序列化
2021-11-22 00:26:45
![](https://img.aspxhome.com/file/2023/3/73063_0s.jpg)
Android提高之MediaPlayer播放网络视频的实现方法
2021-07-03 06:25:29
![](https://img.aspxhome.com/file/2023/0/102600_0s.jpg)
Prometheus 入门教程之SpringBoot 实现自定义指标监控
2022-08-02 12:58:54
![](https://img.aspxhome.com/file/2023/7/67487_0s.jpg)
C#实现上传下载图片
2022-12-15 22:48:22
![](https://img.aspxhome.com/file/2023/3/113873_0s.jpg)
深入讲解C#编程中嵌套类型和匿名类型的定义与使用
2022-12-12 20:48:05
聊聊Spring Cloud Gateway过滤器精确控制异常返回问题
2022-06-23 01:04:14
![](https://img.aspxhome.com/file/2023/5/108215_0s.png)
C#验证身份证的函数
2022-06-16 04:49:37
详解C#实现MD5加密的示例代码
2023-11-28 21:01:54
java中断线程的正确姿势完整示例
2023-01-27 06:33:58
![](https://img.aspxhome.com/file/2023/8/67648_0s.jpg)
Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法
2023-04-05 09:17:52
![](https://img.aspxhome.com/file/2023/4/83114_0s.png)
在IDEA中配置tomcat并创建tomcat项目的图文教程
2023-08-11 11:33:59
![](https://img.aspxhome.com/file/2023/4/78934_0s.png)
Java常用函数式接口总结
2022-02-02 00:02:50
![](https://img.aspxhome.com/file/2023/8/68178_0s.png)
.NET实现父窗体关闭而不影响子窗体的方法
2022-10-06 20:56:04
![](https://img.aspxhome.com/file/2023/4/104264_0s.jpg)
C# Record构造函数的行为更改详解
2022-01-21 04:50:11
android横竖屏切换时候Activity的生命周期
2021-12-10 16:02:36
wpf将表中数据显示到datagrid示例
2023-06-13 04:14:06
C#浅拷贝和深拷贝实例解析
2022-09-03 02:31:05
![](https://img.aspxhome.com/file/2023/5/100135_0s.png)
java中 ${} 和 #{} 有什么区别
2023-11-29 01:34:32
![](https://img.aspxhome.com/file/2023/5/67015_0s.png)