unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
IO流代碼:
void LoadByIO() { float time = Time.time; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); fs.Close(); fs.Dispose(); fs = null; Texture2D t = new Texture2D(1,1); t.LoadImage(bytes); img.texture = t; Debug.Log("IO讀取圖片用時:" + (Time.time-time)); }
WWW代碼:
IEnumerator LoadByWWW() { float time = Time.time; WWW w = new WWW("file://" + path); yield return w; if (string.IsNullOrEmpty(w.error) == false) { Debug.Log("error"); } else { img1.texture = w.texture; } Debug.Log("www讀取圖片用時:" + (Time.time - time)); }
結(jié)果截圖:
補充:unity加載文件的方法-用加載圖片舉例
一、用Resources.Load()方法
1、把圖片(轉(zhuǎn)換或者不轉(zhuǎn)換為sprite都可)放在Resources里
Texture2D imgTexture = Resources.Load("background_one") as Texture2D; Sprite sprite = Sprite.Create(imgTexture, new Rect(0, 0, imgTexture.width, imgTexture.height), new Vector2(0.5f, 0.5f)); Image image = GetComponent<Image>(); image.sprite = sprite;
2、把圖片轉(zhuǎn)換成sprite,放在Resources
//Resources.Load加載圖片默認(rèn)的是Texture2D類型,加了typeof(Sprite)后,就是加載為sprite類型 //然后又轉(zhuǎn)換為object,所以要再用as Sprite轉(zhuǎn)換為Sprite, //如果不加typeof(Sprite),它就是Texture2D轉(zhuǎn)換為object,就不成強制轉(zhuǎn)換為Sprite Image image = GetComponent<Image>(); image.sprite = Resources.Load("background_one", typeof(Sprite)) as Sprite;
二、創(chuàng)建對應(yīng)文件的public變量,然后再unity里把圖片拖給變量賦值
public Sprite play; public Sprite pause; Image image = GetComponent<Image>(); image.sprite = play; image.sprite = pause;
三、用WWW方式,既可以加載網(wǎng)絡(luò)資源,也可以加載本地資源
//用www方式讀取 string path = @"E:\UnityProject\ARVR\Workspace\Test2\Assets\texture\background_one.png"; WWW www = new WWW(path); Texture2D texture = www.texture; Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); Image image = GetComponent<Image>(); image.sprite = sprite;
注意:用www加載,最好使用協(xié)程,等待圖片加載完畢
四、用傳統(tǒng)IO流
//創(chuàng)建文件讀取流 string path = @"E:\UnityProject\ARVR\Workspace\Test2\Assets\texture\background_one.png"; FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); byte[] bye = new byte[fileStream.Length]; fileStream.Read(bye, 0, bye.Length); fileStream.Close(); //創(chuàng)建texture Texture2D texture2D = new Texture2D(240, 144); texture2D.LoadImage(bye); //創(chuàng)建sprite Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f)); Image image = GetComponent<Image>(); image.sprite = sprite;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問題
這篇文章主要介紹了解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問題,具有很好的參考價值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04用C#對ADO.NET數(shù)據(jù)庫完成簡單操作的方法
用C#對ADO.NET數(shù)據(jù)庫完成簡單操作的方法...2007-03-03C#設(shè)計模式之Observer觀察者模式解決牛頓童鞋成績問題示例
這篇文章主要介紹了C#設(shè)計模式之Observer觀察者模式解決牛頓童鞋成績問題,簡單講述了觀察者模式的原理并結(jié)合具體實例形式分析了使用觀察者模式解決牛頓童鞋成績問題的具體步驟相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09c#實現(xiàn)幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入
這篇文章主要介紹了c#實現(xiàn)幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入,主要包括SqlServer、Oracle、SQLite和MySQL,有興趣的可以了解一下。2017-01-01C#使用DateAndTime.DateDiff實現(xiàn)計算年齡
這篇文章主要為大家詳細(xì)介紹了C#如何使用DateAndTime.DateDiff實現(xiàn)根據(jù)生日計算年齡,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01