Unity 從Resources中動(dòng)態(tài)加載Sprite圖片的操作
我就廢話不多說了,大家還是直接看代碼吧~
public Sprite LoadSourceSprite(string relativePath)
{
//Debug.Log("relativePath=" + relativePath);
//把資源加載到內(nèi)存中
Object Preb = Resources.Load(relativePath, typeof(Sprite));
Sprite tmpsprite = null;
try
{
tmpsprite = Instantiate(Preb) as Sprite;
}
catch ( System.Exception ex )
{
}
//用加載得到的資源對象,實(shí)例化游戲?qū)ο螅瑢?shí)現(xiàn)游戲物體的動(dòng)態(tài)加載
return tmpsprite;
//return Resources.Load(relativePath, typeof(Sprite)) as Sprite;
}
補(bǔ)充:Unity運(yùn)行時(shí)動(dòng)態(tài)加載本地圖片
一、Unity運(yùn)行時(shí)加載本地文件夾下所有圖片的方法
用于在使用圖片前加載完成
//引入命名空間
using System;
using System.IO;
/// <summary>
/// 加載圖片的Byte[]數(shù)組
/// </summary>
/// <param name="filesName">地址</param>
public List<byte[]> LoadImage(string filesName)
{
List<byte[]> list = new List<byte[]>();
string tempPath ="E:\"+filesName; // 圖片所在文件夾地址
List<string> filePaths = new List<string>();
string imgtype = "*.BMP|*.JPG|*.PNG";
string[] ImageType = imgtype.Split('|');
for (int i = 0; i < ImageType.Length; i++)
{
//文件夾下所有的圖片路徑
string[] dirs = Directory.GetFiles(tempPath, ImageType[i]);
for (int j = 0; j < dirs.Length; j++)
{
filePaths.Add(dirs[j]);
}
}
for (int i = 0; i < filePaths.Count; i++)
{
byte[] bs = getImageByte(filePaths[i]);
list.Add(bs);
}
return list;
}
#endregion
/// <summary>
/// 根據(jù)圖片路徑返回圖片的字節(jié)流byte[]
/// </summary>
/// <param name="imagePath">圖片路徑</param>
/// <returns>返回的字節(jié)流</returns>
private byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open,FileAccess.Read);
files.Seek(0,SeekOrigin.Begin);
byte[] imgByte = new byte[files.Length];
files.BeginRead(imgByte,0,(int)files.Length,CallBack,files);
return imgByte;
}
/// <summary>
/// 異步加載
/// </summary>
/// <param name="ar"></param>
void CallBack(IAsyncResult ar)
{
FileStream fs = ar.AsyncState as FileStream;
fs.Close();
fs.Dispose();
}
用的時(shí)候:
List<byte[]> data=new List<byte[]>(); //臨時(shí)接收圖片數(shù)據(jù)流
List<Texture2D> turList=new List<Texture2D>(); //保存圖片
data=加載類.LoadImage("測試圖片");
foreach (byte[] item in data)
{
Texture2D tex = new Texture2D(100, 100, TextureFormat.RGBA32, false);
tex.LoadImage(item); //建議哪里調(diào)用哪里轉(zhuǎn) 還可轉(zhuǎn)精靈
turList.Add(tex);
}
二、臨時(shí)加載一張圖片
public static class ImageLoad {
public static Texture2D LoadImageByte(string path){
FileStream files=new FileStream (PathSet.dataPath+path,FileMode.Open,FileAccess.Read);
files.Seek(0,SeekOrigin.Begin);
byte[] imgByte=new byte[files.Length];
//少量臨時(shí)加載會(huì) 紅問號
//files.BeginRead(imgByte,0,(int)files.Length,CallBack,files);
files.Read(imgByte,0,imgByte.Length);
files.Close();
Texture2D tx=new Texture2D (512,512);
tx.LoadImage(imgByte);
return tx;
}
static void CallBack(IAsyncResult ar){
FileStream fileStream=ar.AsyncState as FileStream;
fileStream.Close();
fileStream.Dispose();
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Unity實(shí)現(xiàn)Flappy Bird游戲開發(fā)實(shí)戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)Flappy Bird游戲開發(fā)實(shí)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
WPF利用CommunityToolkit.Mvvm實(shí)現(xiàn)級聯(lián)選擇器
這篇文章主要介紹了WPF如何利用CommunityToolkit.Mvvm實(shí)現(xiàn)級聯(lián)選擇器,文中的示例代碼講解詳細(xì),對我們的學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-12-12
C#使用Dns類實(shí)現(xiàn)查詢主機(jī)名對應(yīng)IP地址
C#中的Dns類能夠與默認(rèn)的DNS服務(wù)器進(jìn)行通信,以檢索IP地址,這篇文章主要介紹了C#如何使用Dns類解析出主機(jī)對應(yīng)的IP地址信息,需要的可以參考下2024-02-02
C# 復(fù)制指定節(jié)點(diǎn)的所有子孫節(jié)點(diǎn)到新建的節(jié)點(diǎn)下
這篇文章主要介紹了C# 復(fù)制指定節(jié)點(diǎn)的所有子孫節(jié)點(diǎn)到新建的節(jié)點(diǎn)下的相關(guān)資料,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
C# httpwebrequest訪問HTTPS錯(cuò)誤處理方法
下面小編就為大家?guī)硪黄狢# httpwebrequest訪問HTTPS錯(cuò)誤處理方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

