unity3d 上传本地PC图片
2015-05-31 14:17
417 查看
网上有两个常用的方法,我先用了第一种。第一种方法:需要注意三点,首先导入system.window.Forms.dll库,导进去后一定记得要修改playersetting下的optimization->API compatibility level为.NET 2.0,至于为什么改成这样,可以参考unity3d 的解释:http://game.ceeger.com/Manual/MobileDotnet.html。还要注意一点:加载的本地图片地址一定不能带有中文路径另外,www新建的时候要加上“file://” + url;
WWW www = new WWW("file://" + url); //注意要加上"file://"只需要一个脚本,其中代码如下:
using UnityEngine;using System.Collections;using System.Windows.Forms;using System.IO;public class UploadImg: MonoBehaviour { public Texture2D img = null; void OnGUI() { if (GUI.Button(new Rect(0, 0, 100, 20), "选择文件")) { 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(od.FileName)); } } if (img != null) { GUI.DrawTexture(new Rect(0, 20, img.width/2, img.height/2), img); } } IEnumerator GetTexture(string url) { WWW www = new WWW("file://" + url); //注意要加上"file://" yield return www; if (www.isDone) { img = www.texture; } } }效果是这样的,但是感觉和win7风格差的太远第二种方法看起来要炫多了:需要两个脚本。OpenFileName.cs如下:
using UnityEngine; using System.Collections; using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenFileName { public int structSize = 0; public IntPtr dlgOwner = IntPtr.Zero; public IntPtr instance = IntPtr.Zero; public String filter = null; public String customFilter = null; public int maxCustFilter = 0; public int filterIndex = 0; public String file = null; public int maxFile = 0; public String fileTitle = null; public int maxFileTitle = 0; public String initialDir = null; public String title = null; public int flags = 0; public short fileOffset = 0; public short fileExtension = 0; public String defExt = null; public IntPtr custData = IntPtr.Zero; public IntPtr hook = IntPtr.Zero; public String templateName = null; public IntPtr reservedPtr = IntPtr.Zero; public int reservedInt = 0; public int flagsEx = 0; } public class WindowDll { [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] OpenFileName ofn); public static bool GetOpenFileName1([In, Out] OpenFileName ofn) { return GetOpenFileName(ofn); } }UploadImage.cs如下:
using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class Upload : MonoBehaviour { public Texture2D img = null; private bool click = false; void OpenDialog() { click = true; } void EnterImage() { 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; ofn.initialDir = UnityEngine.Application.dataPath;//默认路径 ofn.title = "Open Project"; ofn.defExt = "JPG";//显示文件的类型 //注意 一下项目不一定要全选 但是0x00000008项不要缺少 ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR if(WindowDll.GetOpenFileName(ofn)) { StartCoroutine(GetTexture(ofn.file)); } } IEnumerator GetTexture(string url) { WWW wwwTexture = new WWW ("file://" + url); yield return wwwTexture; if(wwwTexture.isDone) img = wwwTexture.texture; } void OnGUI() { if(click) { EnterImage(); click = false; } if (img != null) GUI.DrawTexture (new Rect(Screen.width/2 - img.width/2,Screen.height/2 - img.height,img.width,img.height),img); } }
相关文章推荐
- Unity3d在安卓平台下使用sqlite存储操作数据
- 使用Unity编写传统ARPG游戏的人物操作方式
- unity, 查看资源文件类型
- Unity3D占用内存太大的解决方法
- Unity 3D中的内存管理
- Unity跑酷游戏中的路点生成算法
- C# Unity游戏开发——Excel中的数据是如何到游戏中的 (二)
- [Unity基础]自带寻路Navmesh
- Unity 方向控制及鼠标滚轮代码控制
- unity3d优化总结篇
- 【整理】unity3d优化总结篇
- 全体听令!Unity3D崩溃分析援军已到,集中火力消灭所有手游崩溃!
- 【Unity技巧】四元数(Quaternion)和旋转
- Unity3D中uGUI事件系统简述及使用方法总结
- Unity3D面试题整合
- Unity3d制作一个简单的打开保存对话框-按钮2
- unity3d 在shader中创建及使用Texture3D
- unity3d 协程Coroutines
- Unity 编辑器下控制粒子播放跟随位移
- unity资源打包研究