中文字幕人妻少妇伦伦伦,0中文字幕乱妇无码av在线,中文字幕日韩无码一区
當(dāng)前位置: 主頁 > 其它 > 日志 >

unity3d加載外部圖片

來源: 瀏覽數(shù):
責(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)用獲取到圖片的信息了,剩下的大家都知道怎么做了。
免責(zé)聲明:本文僅代表作者個人觀點,與納金網(wǎng)無關(guān)。其原創(chuàng)性以及文中陳述文字和內(nèi)容未經(jīng)本站證實,對本文以及其中全部或者部分內(nèi)容、文字的真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,并請自行核實相關(guān)內(nèi)容。



  • TAGS:
  • 網(wǎng)友評論

    您需要登錄后才可以發(fā)帖 登錄 | 立即注冊

    關(guān)閉

    全部評論:0條

    聯(lián)系方式

    服務(wù)熱線:15059788121 / 13489872927 / 15959158412

    換鏈Q(jìng)Q:943169942

    電子郵箱:943169942@qq.com

    聯(lián)系地址:福建省晉江國際工業(yè)設(shè)計園5號樓

    福建省信芯長盈科技有限公司 閩ICP備2021016425號-2/3

    Copyright trusteddivorcelawyers.com 2008-2025 All Rights Reserved

    開展“凈網(wǎng)2019"專項行動,堅決保障網(wǎng)絡(luò)空間清朗

    IOS版

    安卓版

    官網(wǎng)認(rèn)證