查看: 7815|回复: 3
打印 上一主题 下一主题

unity3d加载外部图片

[复制链接]

10

主题

2

听众

1106

积分

助理设计师

吴队

Rank: 4

纳金币
85
精华
1
跳转到指定楼层
楼主
发表于 2013-1-26 09:22:27 |只看该作者 |倒序浏览
最近因为需求加载unity外部图片,所以就小研究了下,下面是自己尝试的集中方法,包括发布***、web、以及Flash三个平台的测试(皆是通过读取XML配置文件加载):第一种方法:通过Resources.Load()加载       这个方法是unity内部提供的一个动态加载的方法,但是经过测试发现,放入unity内部Resources文件下的所有图片发布出来之后都是经过编译的,也就是说我没法在发布出来的文件进行随意的更改我想要显示的图片,这样通过XML配置文件读取就没有任何意思了,所以自己放弃了这种方法。第二种方法:通过WWW类加载    这个类也是unity3d内部提供的加载,通过这个类的调用,我们一方面是可以加载本地的图片,一方面也可以加载网络上的图片,所以这为我们做图片的动态加载提供了很好的解决方案,方法如下:  这个是我的XML配置文件:  <config>
<photos icon = "Smallsmall_1" original = "Originaloriginal_1" ></photos>
<photos icon = "Smallsmall_2" original = "Originaloriginal_2" ></photos>
<photos icon = "Smallsmall_3" original = "Originaloriginal_3" ></photos>
<photos icon = "Smallsmall_4" original = "Originaloriginal_4" ></photos>
<photos icon = "Smallsmall_5" original = "Originaloriginal_5" ></photos>
<photos icon = "Smallsmall_6" original = "Originaloriginal_6" ></photos>
<photos icon = "Smallsmall_7" original = "Originaloriginal_7" ></photos>
<photos icon = "Smallsmall_8" original = "Originaloriginal_8" ></photos>
</config>一下是我读取的代码using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;public class ThurmilUI3 : MonoBehaviour {
           private Texture[] icon;
           private Texture[] originalPhoto;
           private string xmlPath = @"/config.xml";
           private string photoPath = @"/photos/";
           private string iconPath = @"/photos/";
           private string tempPath = "";
           private WWW www;
           IEnumerator Start()
           {
                 xmlPath = Application.dataPath +@"/.."+ xmlPath;
                 photoPath ="file://"+ Application.dataPath + @"/.."+ photoPath;
                 iconPath ="file://"+ Application.dataPath + @"/.."+iconPath;
                 if(File.Exists(xmlPath))
                 {
                         XmlDocument xmlDoc = new XmlDocument();
                         xmlDoc.Load(xmlPath);
                         XmlNodeList nodeList = xmlDoc.SelectSingleNode("config").ChildNodes;
                         PrcNum = nodeList.Count;
                         icon = new Texture[PrcNum];
                         originalPhoto = new Texture[PrcNum];
                         int j = 0;
                        foreach(XmlElement xe in nodeList)
                       {
                            Debug.Log("index of image: "+j);
                            tempPath = iconPath + xe.GetAttribute("icon")+".jpg";
                             debugMes = tempPath;
                             www = new WWW(tempPath);
                             yield return www;
                            if(www.isDone)
                           {
                                  icon[j] =www.texture;
                                  if(icon[j] != null)
                                      Debug.Log("Load "+tempPath+" success");
                                 else
                                      Debug.Log("Not Found "+tempPath);
                           }
                           tempPath = photoPath + xe.GetAttribute("original")+".jpg";
                           www = new WWW(tempPath);
                          yield return www;
                           if(www.isDone)
                           {
                                 originalPhoto[j]=www.texture;
                                if(originalPhoto[j]!= null)
                                       Debug.Log("Load "+tempPath+" success");
                                else
                                       Debug.Log("Not Found "+tempPath);
                         }
                          j++;
   
             }
       }
     else
    {
           debugMes = "xmlPath is not found";
           Debug.LogError("xmlPath is not found");
           return false;
     }
  
   }void Update(){        //在这里我们就可以做我的自己想做的事了}
}第三种方法:通过引入System.Drawing的Image类加载显示图片:方法基本跟上面一样,就说下核心部分:首先我们可以通过Image image = Image.FromFile(ImagePath);来加载一个图片或者是通过    FileStream fs = new FileStream(tempPath,FileMode.Open,FileAccess.Read);      Image image = Image.FromStream(fs);来加载图片以上方法加载的图片都是一个Image类的属性,跟我们unity3d中使用的Texture2D没法联系上,经过自己的思考,首先我们可以通过把image加载进来的图片数据信息提取出来,然后再通过Texture2D.LoadImage(byte[] bytes);来转换到我们再unity中可以使用的Texture2D类,所以我们首先要编写个获取image图片数据的方法:
public byte[] imageToByteArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png); return ms.ToArray(); }通过本方法我们就可以应用获取到图片的信息了,剩下的大家都知道怎么做了。测试效果:
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-1-28 18:31:47 |只看该作者
楼主的帖子不错呀!支持了!
回复

使用道具 举报

Zack    

459

主题

1

听众

5478

积分

高级设计师

Rank: 6Rank: 6

纳金币
5531
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-1-29 00:03:57 |只看该作者
这个好,正好需要用到,谢谢!
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2013-1-29 22:02:09 |只看该作者
希望更多的朋友分享制作经验
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 18:26 , Processed in 0.090933 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部