unity从本地选择图片并上传
unity實現打開文件夾選擇圖片的兩種方式:
一、引用System.Windows.Forms
此方法需要在unity的安裝目錄中找到System.Windows.Forms.dll文件并放在項目的Plugins文件夾中。注意:此方法在打包webgl版本時會報錯。
using System.IO;
using System.Windows.Forms;
?public void CreatChartImgPanel()
?{
? ? ? ? ? OpenFileDialog od = new OpenFileDialog();
? ? ? ? ? ?od.Title = "請選擇圖片";
? ? ? ? ? ?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";
? ? ? ? ? ? //注意 一下項目不一定要全選 但是0x00000008項不要缺少 ?
? ? ? ? ? ? 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;
? ? ? ? }
?}
總結
以上是生活随笔為你收集整理的unity从本地选择图片并上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三十一、Java图形化界面设计——布局管
- 下一篇: 知识分享 | 精益的力量之:标准作业