unity3d加載外部圖片
責(zé)任編輯:3D數(shù)媒 時(shí)間:2013-01-25 17:53
[導(dǎo)讀]
最近因?yàn)樾枨蠹虞dunity外部圖片,所以就小研究了下,下面是自己嘗試的集中方法,包括發(fā)布***、web、以及Flash三個(gè)平臺(tái)的測(cè)試(皆是通過(guò)讀取XML配置文件加載):
第一種方法:通過(guò)Resources.Load()加載
這個(gè)方法是unity內(nèi)部提供的一個(gè)動(dòng)態(tài)加載的方法,但是經(jīng)過(guò)測(cè)試發(fā)現(xiàn),放入unity內(nèi)部Resources文件下的所有圖片發(fā)布出來(lái)之后都是經(jīng)過(guò)編譯的,也就是說(shuō)我沒(méi)法在發(fā)布出來(lái)的文件進(jìn)行隨意的更改我想要顯示的圖片,這樣通過(guò)XML配置文件讀取就沒(méi)有任何意思了,所以自己放棄了這種方法。
第二種方法:通過(guò)WWW類加載
這個(gè)類也是unity3d內(nèi)部提供的加載,通過(guò)這個(gè)類的調(diào)用,我們一方面是可以加載本地的圖片,一方面也可以加載網(wǎng)絡(luò)上的圖片,所以這為我們做圖片的動(dòng)態(tài)加載提供了很好的解決方案,方法如下:
這個(gè)是我的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()
{
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()
{
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()
{
//在這里我們就可以做我的自己想做的事了
}
}
第三種方法:通過(guò)引入System.Drawing的Image類加載顯示圖片:
方法基本跟上面一樣,就說(shuō)下核心部分:
首先我們可以通過(guò)Image image = Image.FromFile(ImagePath);來(lái)加載一個(gè)圖片
或者是通過(guò) FileStream fs = new FileStream(tempPath,FileMode.Open,FileAccess.Read);
Image image = Image.FromStream(fs);來(lái)加載圖片
以上方法加載的圖片都是一個(gè)Image類的屬性,跟我們unity3d中使用的Texture2D沒(méi)法聯(lián)系上,經(jīng)過(guò)
自己的思考,首先我們可以通過(guò)把image加載進(jìn)來(lái)的圖片數(shù)據(jù)信息提取出來(lái),然后再通過(guò)Texture2D.LoadImage(byte[] bytes);
來(lái)轉(zhuǎn)換到我們?cè)賣nity中可以使用的Texture2D類,
所以我們首先要編寫個(gè)獲取image圖片數(shù)據(jù)的方法:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
通過(guò)本方法我們就可以應(yīng)用獲取到圖片的信息了,剩下的大家都知道怎么做了。- 分享到:
- 有巨蟹座男朋友的女2017年06月21日
- 高校人才引進(jìn)看重“2016年08月23日
- 視覺(jué)設(shè)計(jì)該如何賦予2016年08月23日
- 如何創(chuàng)造設(shè)計(jì)經(jīng)典2016年08月23日
- MLikeasong答互聯(lián)網(wǎng)軟件2016年08月19日
- 工業(yè)設(shè)計(jì)創(chuàng)業(yè)的誤區(qū)2016年08月18日
- 2016年北京理工大學(xué)工2015年08月10日
- 中國(guó)一線城市近8成白2015年08月04日
- 2010宅男腐女們的杯洗2015年08月04日
- 深圳長(zhǎng)途搬家公司包2013年03月22日
您需要登錄后才可以發(fā)帖 登錄 | 立即注冊(cè)
- 用戶名:
- 密 碼:
- 驗(yàn)證碼:
看不清? 點(diǎn)擊更換
- 忘記密碼?
全部評(píng)論:0條
-
谷歌眼鏡專題 書簽設(shè)計(jì)專題 教師節(jié)禮物專題 cs模型專題 服裝設(shè)計(jì)專題 太師椅3d模型專題 環(huán)藝設(shè)計(jì)專題 德國(guó)IF設(shè)計(jì)獎(jiǎng)專題 壁燈3D模型專題 手冊(cè)設(shè)計(jì)專題 浴缸3d模型下載專題 手機(jī)支架專題 創(chuàng)意攝影作品專題 創(chuàng)意舊物改造專題 創(chuàng)意圣誕禮物專題 ui設(shè)計(jì)專題 獎(jiǎng)杯3d模型專題 3dmax8.0下載專題 角色動(dòng)作專題 耳機(jī)設(shè)計(jì)專題