来自:http://www.cnblogs.com/kex1n/p/3415332.html
Unity3D论坛最近因为需求加载unity外部图片,所以就小研究了下,下面是自己尝试的集中方法,包括发布的exe、web、以及Flash三个平台的测试(皆是通过读取XML配置文件加载):
第一种方法:通过Resources.Load()加载
这个方法是unity内部提供的一个动态加载的方法,但是经过测试发现,放入unity内部Resources文件下的所有图片发布出来之后都是经过编译的,也就是说我没法在发布出来的文件进行随意的更改我想要显示的图片,这样通过XML配置文件读取就没有任何意思了,所以自己放弃了这种方法。
第二种方法:通过WWW类加载
这个类也是unity3d内部提供的加载,通过这个类的调用,我们一方面是可以加载本地的图片,一方面也可以加载网络上的图片,所以3d软件这为我们做图片的动态加载提供了很好的解决方案,方法如下:
这个是我的XML配置文件:
<config>
<photos icon = "Small\\small_1" original = "Original\\original_1" ></photos>
<photos icon = "Small\\small_2" original = "Original\\original_2" ></photos>
<photos icon = "Small\\small_3" original = "Original\\original_3" ></photos>
<photos icon = "Small\\small_4" original = "Original\\original_4" ></photos>
<photos icon = "Small\\small_5" original = "Original\\original_5" ></photos>
<photos icon = "Small\\small_6" original = "Original\\original_6" ></photos>
<photos icon = "Small\\small_7" original = "Original\\original_7" ></photos>
<photos icon = "Small\\small_8" original = "Original\\original_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();
- }