今天來說說香椎,unity加載pc端圖片和手機端圖片的兩種方法: WWW加載 和 傳統(tǒng)IO加載
- 首先說說 WWW 加載
public void loadByWww(){
StartCoroutine (Load());
}
IEnumerator Load()
{
double startTime = (double)Time.time;
//+ currentIndex.ToString ("D5")
WWW www = new WWW ("file://F:\\imageTest\\jiaoao1-2guodu" + currentIndex.ToString ("D5") + ".jpg" );
yield return www;
if(www != null && string.IsNullOrEmpty(www.error)){
GetComponent<Renderer>().material.mainTexture = www.texture;
}
}
首先www 既可以加載網(wǎng)絡(luò)圖片 也可以加載本地圖片 本地圖片路徑:
1.pc端本地 : file://F:\imageTest\jiaoao1-2guodu.png
2.android手機端SD卡:/storage/emulated/0/ImageTest/jiaoao1-2guodu.png
2.傳統(tǒng)IO流加載
FileStream fileStream = new FileStream("F:\\imageTest\\jiaoao1-2guodu"+ currentIndex.ToString ("D5") + ".jpg", FileMode.Open, FileAccess.Read);
// FileStream fileStream = new FileStream("/storage/emulated/0/ImageTest/jiaoao1-2guodu" + currentIndex.ToString ("D5") + ".jpg" , FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//創(chuàng)建文件長度緩沖區(qū)
byte[] bytes = new byte[fileStream.Length];
//讀取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//釋放文件讀取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//創(chuàng)建Texture
int width=1280;
int height=800;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
GetComponent<Renderer>().material.mainTexture = texture;
傳統(tǒng)IO流加載本地圖片路徑:
1.pc端本地 :F:\imageTest\jiaoao1-2guodu.png
2.android手機端SD卡:/storage/emulated/0/ImageTest/jiaoao1-2guodu.png
總結(jié):
經(jīng)測試IO加載要比WWW加載更快大猛,效率更高。