加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Unity-WWW file协议路径问题

(2016-10-25 20:21:26)
标签:

unity

分类: Unity引擎功能

作者一直在做unity的编辑器扩展与中文脚本组件开发~
今天遇到一个需求:点击打开按钮,选择磁盘任意路径的一张图片,然后显示在窗口~

实现原理也很简单:使用 WWW + file:// 协议完成图片从任意路径的加载,其中图片保存在Texture2D中;

代码写完在测试打开图片时,发现一个奇怪的问题:在桌面的上的图片无法加载显示,表现为图片显示的是一个白地的红色问号图片!如下图:

http://img.manew.com/data/attachment/forum/201610/21/164556xhvz6zuhillewh60.png.thumb.jpgfile协议路径问题" TITLE="Unity-WWW file协议路径问题" />


之后将同样的图片放到C盘、D盘、E盘根目录下测试,表现均为加载失败~但是奇怪的是放在 F盘根目录下图片却可以正常加载显示!!!

为什么?为什么?为什么?http://www.manew.com/static/image/smiley/default/20119015.giffile协议路径问题" />

想到我的工程目录也是F盘的,以前类似的需求加载的图片也是在工程资源文件夹内??难道WWW不可以加载其他盘问的文件!!!!

为什么?为什么?为什么? http://www.manew.com/static/image/smiley/default/20119001.giffile协议路径问题" />

突然想到,传入的文件磁盘路径有问题?那是什么问题了?~~

经过测试发现,如果传入的文件磁盘路径分隔符是 “/”  那么就会发生作者刚才描述的情况,只能加载工程同盘符的图片文件!

解决方法是,将路径中的  “/”  替换为 "\" (即 斜杠 替换为 反斜杠 类型的路径分隔符) ,就可以加载其他盘符的图片文件了!http://www.manew.com/static/image/smiley/default/20119000.giffile协议路径问题" />

假设工程文件在F盘,期望加载1.jpg的图片并且图片在各盘符根目录下均存在;
结果如下表:
编号 路径 结果
1 file://C:/1.jpg
file://D:/1.jpg
file://E:/1.jpg
加载异常
2 file://F:/1.jpg 与工程同目录
加载正常
3 file://C:\1.jpg
file://D:\1.jpg
file://E:\1.jpg
file://F:\1.jpg
加载正常

注:以上测试环境为 window 7 x64 、unity 5.3.4f1
以下为测试代码:
  • using UnityEditor;using System;
  • using System.Collections;
  • using System.Collections.Generic;
  •  
  • public class TestWindow : EditorWindow
  •     {
  •         public string filePath = "";
  •  
  •         public Texture2D texture = null;
  •  
  •         public string[] fileFilters = new string[] { "jgp", "jpg", "png", "png", "bmp", "bmp", "tif", "tif", "tiff", "tiff", "所有文件", "*" };
  •  
  •         [MenuItem("Test/测试窗口")]
  •         public static void Init()
  •         {
  •             TestWindow window = (TestWindow)EditorWindow.GetWindow(typeof(TestWindow));
  •             window.Show();
  •         }
  •  
  •         public void OnEnable()
  •         {
  •             this.titleContent = new GUIContent("测试窗口");
  •         }
  •  
  •         public Texture2D LoadTextureFromLocalDisk(string path)
  •         {
  •             WWW www = new WWW("file://" + path);
  •             if (www != null)
  •             {
  •                 return www.texture;
  •             }
  •             return null;
  •         }
  •  
  •         public void OnGUI()
  •         {
  •             if (GUILayout.Button("加载图片0"))
  •             {
  •                 if (string.IsNullOrEmpty(filePath)) filePath = Application.dataPath;
  •                 filePath = EditorUtility.OpenFilePanelWithFilters("选择图片", System.IO.Path.GetDirectoryName(filePath), fileFilters);
  •                 if (System.IO.File.Exists(filePath))
  •                 {
  •                     Debug.Log("加载图片 0: " + filePath);
  •                     if (texture) UnityEngine.Object.DestroyImmediate(texture);
  •                     texture = LoadTextureFromLocalDisk(filePath);
  •                 }
  •             }
  •             if (GUILayout.Button("加载图片1 - 将路径信息中 / 替换为 \"))
  •             {
  •                 if (string.IsNullOrEmpty(filePath)) filePath = Application.dataPath;
  •                 filePath = EditorUtility.OpenFilePanelWithFilters("选择图片", System.IO.Path.GetDirectoryName(filePath), fileFilters);
  •                 if (System.IO.File.Exists(filePath))
  •                 {
  •                     filePath = filePath.Replace("/", "\");
  •                     Debug.Log("加载图片 1: " + filePath);
  •                     if (texture) UnityEngine.Object.DestroyImmediate(texture);
  •                     texture = LoadTextureFromLocalDisk(filePath);
  •                 }
  •             }
  •             if (texture) GUILayout.Box(texture, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
  •         }
  •     }

原文连接:http://www.manew.com/thread-96345-1-1.html

0

阅读 收藏 喜欢 打印举报/Report
前一篇:Android广播
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有