unity3d加載外部圖片
責(zé)任編輯:3D數(shù)媒 時間:2013-01-25 17:53
[導(dǎo)讀]
最近因為需求加載unity外部圖片,所以就小研究了下,下面是自己嘗試的集中方法,包括發(fā)布***、web、以及Flash三個平臺的測試(皆是通過讀取XML配置文件加載):
第一種方法:通過Resources.Load()加載
這個方法是unity內(nèi)部提供的一個動態(tài)加載的方法,但是經(jīng)過測試發(fā)現(xiàn),放入unity內(nèi)部Resources文件下的所有圖片發(fā)布出來之后都是經(jīng)過編譯的,也就是說我沒法在發(fā)布出來的文件進(jìn)行隨意的更改我想要顯示的圖片,這樣通過XML配置文件讀取就沒有任何意思了,所以自己放棄了這種方法。
第二種方法:通過WWW類加載
這個類也是unity3d內(nèi)部提供的加載,通過這個類的調(diào)用,我們一方面是可以加載本地的圖片,一方面也可以加載網(wǎng)絡(luò)上的圖片,所以這為我們做圖片的動態(tài)加載提供了很好的解決方案,方法如下:
這個是我的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()
{
//在這里我們就可以做我的自己想做的事了
}
}
第三種方法:通過引入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沒法聯(lián)系上,經(jīng)過
自己的思考,首先我們可以通過把image加載進(jìn)來的圖片數(shù)據(jù)信息提取出來,然后再通過Texture2D.LoadImage(byte[] bytes);
來轉(zhuǎn)換到我們再unity中可以使用的Texture2D類,
所以我們首先要編寫個獲取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();
}
通過本方法我們就可以應(yīng)用獲取到圖片的信息了,剩下的大家都知道怎么做了。- 分享到:
- 有巨蟹座男朋友的女2017年06月21日
- 高校人才引進(jìn)看重“2016年08月23日
- 視覺設(shè)計該如何賦予2016年08月23日
- 如何創(chuàng)造設(shè)計經(jīng)典2016年08月23日
- MLikeasong答互聯(lián)網(wǎng)軟件2016年08月19日
- 工業(yè)設(shè)計創(chuàng)業(yè)的誤區(qū)2016年08月18日
- 2016年北京理工大學(xué)工2015年08月10日
- 中國一線城市近8成白2015年08月04日
- 2010宅男腐女們的杯洗2015年08月04日
- 深圳長途搬家公司包2013年03月22日