我就废话不多说了,大家还是直接看代码吧~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public Sprite LoadSourceSprite( string relativePath) { //Debug.Log("relativePath=" + relativePath); //把资源加载到内存中 Object Preb = Resources.Load(relativePath, typeof (Sprite)); Sprite tmpsprite = null ; try { tmpsprite = Instantiate(Preb) as Sprite; } catch ( System.Exception ex ) { } //用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载 return tmpsprite; //return Resources.Load(relativePath, typeof(Sprite)) as Sprite; } |
补充:Unity运行时动态加载本地图片
一、Unity运行时加载本地文件夹下所有图片的方法
用于在使用图片前加载完成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
//引入命名空间 using System; using System.IO; /// <summary> /// 加载图片的Byte[]数组 /// </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> /// 根据图片路径返回图片的字节流byte[] /// </summary> /// <param name="imagePath">图片路径</param> /// <returns>返回的字节流</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(); } |
用的时候:
1
2
3
4
5
6
7
8
9
|
List< byte []> data= new List< byte []>(); //临时接收图片数据流 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); //建议哪里调用哪里转 还可转精灵 turList.Add(tex); } |
二、临时加载一张图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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]; //少量临时加载会 红问号 //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(); } } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/wks310/article/details/86061768