日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unity从本地选择图片并上传

發(fā)布時(shí)間:2024/1/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity从本地选择图片并上传 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

unity實(shí)現(xiàn)打開文件夾選擇圖片的兩種方式:

一、引用System.Windows.Forms

此方法需要在unity的安裝目錄中找到System.Windows.Forms.dll文件并放在項(xiàng)目的Plugins文件夾中。注意:此方法在打包webgl版本時(shí)會(huì)報(bào)錯(cuò)。

using System.IO;
using System.Windows.Forms;

?public void CreatChartImgPanel()
?{
? ? ? ? ? OpenFileDialog od = new OpenFileDialog();
? ? ? ? ? ?od.Title = "請(qǐng)選擇圖片";
? ? ? ? ? ?od.Multiselect = false;
? ? ? ? ? ?od.Filter = "圖片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";

? ? ? ? ? ?if (od.ShowDialog() == DialogResult.OK)
? ? ? ? ? ?string TexPath = od.FileName;

? ? ? ? ? ? if (!File.Exists(TexPath))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ?Debug.Log("加載失敗");
? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? }

? ? ? ? ? ? //WWW方式加載圖片
? ? ? ? ? ? StartCoroutine(WWW_Tex("file://" + TexPath));? ? ? ? ? ??
? ? ? ? }

二、引用System.Runtime.InteropServices

此方法PC、webgl均可使用

using System.IO;

using System.Runtime.InteropServices;?

public void CreatChartImgPanel()
? ? ? ? {? ? ??

? ? ? ? ? ? OpenFileName ofn = new OpenFileName();
? ? ? ? ? ? ofn.structSize = Marshal.SizeOf(ofn);
? ? ? ? ? ? ofn.filter = "All Files\0*.*\0\0";
? ? ? ? ? ? ofn.file = new string(new char[256]);
? ? ? ? ? ? ofn.maxFile = ofn.file.Length;
? ? ? ? ? ? ofn.fileTitle = new string(new char[64]);
? ? ? ? ? ? ofn.maxFileTitle = ofn.fileTitle.Length;
? ? ? ? ? ? string path = Application.streamingAssetsPath;
? ? ? ? ? ? path = path.Replace('/', '\\');
? ? ? ? ? ? ofn.initialDir = path;
? ? ? ? ? ? ofn.title = "Open Project";
? ? ? ? ? ? ofn.defExt = "JPG";
? ? ? ? ? ? //注意 一下項(xiàng)目不一定要全選 但是0x00000008項(xiàng)不要缺少 ?
? ? ? ? ? ? ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;??
? ? ? ? ? ? if (WindowDll.GetOpenFileName(ofn))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Debug.Log("Selected file with full path:" + ofn.file);
? ? ? ? ? ? }
? ? ? ? ? ? if (ofn.file != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? StartCoroutine(WWW_Tex(ofn.file));
? ? ? ? ? ? }
? ? ? ? }

?選擇完然后加載本地圖片

Texture?wwwTexture;

IEnumerator WWW_Tex(string url)
{
? ? ? ? WWW www = new WWW(url);
? ? ? ? yield return www;
? ? ? ??if (www.isDone && www.error == null)
? ? ? ??{
? ? ? ? ? ? ? ? wwwTexture = www.texture;
? ? ? ? }
?}

總結(jié)

以上是生活随笔為你收集整理的unity从本地选择图片并上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。