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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165using System; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// IO加载本地图片 txt文档 /// </summary> public class ComPanyUILoadController : MonoBehaviour { public static ComPanyUILoadController instance { get; private set; } private string jsonPath = @"E:文件夹名json存放文件夹json名.json"; //json文件路径 private string imagePath = @"E:文件夹名文件夹名"; //图片文件路径 public List<CompanyData> datas; //数据存储列表 public CompanyData currentDatas; //Json解析格式 private void Awake() { instance = this; //加载各个文件 datas = JsonToObject.JsonToObject_ByJsonFile<CompanyData>(jsonPath); //json解析 } #region 图片加载 /// <summary> /// 获取文件根路径 /// </summary> /// <returns></returns> private string GetPath() { string path =""; foreach (CompanyData i in datas) { if (GameManager.instacne.currentCompany.Equals(i.ID)) //加载编号 { path = imagePath + i.Name+@""; } } return path; // E:文件夹名文件夹名文件夹名 } /// <summary> /// 按照表编号加载对象 /// </summary> /// <returns></returns> public CompanyData LoadText() { foreach (var i in datas) { if (i.ID == null) { break; } if (i.ID.Equals(GameManager.instacne.currentCompany)) { currentDatas = i; } } return currentDatas; } /// <summary> ///加载一个文件夹中所有的图片流 返回字典 /// 加载图片的Dictionary<string,List<Byte[]>> /// </summary> /// <param name="filesName">图片所在文件夹地址</param> /// <returns></returns> public Dictionary<string, List<byte[]>> LoadImages(string filesName) { Dictionary<string, List<byte[]>> list = new Dictionary<string, List<byte[]>>(); string tempPath = GetPath() + 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++) { //切割图片名和对对应的图片流数据 List<byte[]> tl = new List<byte[]>(); string Tstr = filePaths[i].Split('\')[5].Split('.')[0]; //图片位置 下标为5个元素是图片名称 分割图片名称 byte[] bs = getImageByte(filePaths[i]); tl.Add(bs); list.Add(Tstr,tl); //图片名和图片流数据对应 } return list; } #region 加载图片List<Texture2D> /// <summary> ///加载文件夹下所有图片流 返回图片流列表 /// 加载图片的Byte[]数组 /// </summary> /// <param name="filesName">地址</param> public List<byte[]> LoadImage(string filesName) { List<byte[]> list = new List<byte[]>(); string tempPath = GetPath() + 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(); } #endregion }
用的时候:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13List<byte[]> data=new List<byte[]>(); //临时接收图片数据流 List<Texture2D> turList=new List<Texture2D>(); //保存图片 data=ComPanyUILoadController.instance.LoadImage("图片所在文件夹"); foreach (byte[] item in data) { //文件byte[]转换成Texture2D Texture2D tex = new Texture2D(100, 100, TextureFormat.RGBA32, false); tex.LoadImage(item); //建议哪里调用哪里转 turList.Add(tex); } //后续或将Texture2D转换为Sprite使用
二、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
27public static class ImageLoad { public static Texture2D LoadImageByte(string path) { //读取图片位置 PathSet.dataPath+path=图片所在文件夹路径+图片名称.格式 .png/.jpg等 FileStream files=new FileStream (PathSet.dataPath+path,FileMode.Open,FileAccess.Read); files.Seek(0,SeekOrigin.Begin); byte[] imgByte=new byte[files.Length]; //这种加载方法临时加载可能会加载数据流不全,在转成Texture2D出现红问号加载失败的情况 //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(); } }
最后
以上就是哭泣小甜瓜最近收集整理的关于Unity动态加载本地图片的全部内容,更多相关Unity动态加载本地图片内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复