UGUI获取图集工具类

因为NGUI内部已经封装了方法,获取图集比较容易,但是UGUI需要自己来封装,所以动态设置图集的时候就比较麻烦,所以简单写了一个方法,这里记录一下以后直接拿来用就可以了:

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
 // 记录图集
private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
// 记录sprite
private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

public static Sprite LoadSprites(string atlasName, string spriteName)
{
Sprite spriteOntology = null;
spriteDic.TryGetValue(spriteName, out spriteOntology);

if (spriteOntology == null)
{
Sprite[] atlasArry = null;
recordAtlasDic.TryGetValue(atlasName, out atlasArry);
if (atlasArry != null)
{
atlasArry = recordAtlasDic[atlasName];
}
else
{
try
{
atlasArry = Resources.LoadAll(atlasName);
recordAtlasDic.Add(atlasName, atlasArry);
}
catch
{
throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
}
}

try
{
for (int i = 0; i < atlasArry.Length; i++)
{
string key = atlasArry[i].name;
spriteDic.Add(key, atlasArry[i]);
}
spriteOntology = spriteDic[spriteName];
}
catch
{
throw new Exception(string.Format("No atlas was found:{0}", atlasName));
}
}
return spriteOntology;
}
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
 // 记录图集
private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
// 记录sprite
private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

public static Sprite LoadSprites(string atlasName, string spriteName)
{
Sprite spriteOntology = null;
spriteDic.TryGetValue(spriteName, out spriteOntology);

if (spriteOntology == null)
{
Sprite[] atlasArry = null;
recordAtlasDic.TryGetValue(atlasName, out atlasArry);
if (atlasArry != null)
{
atlasArry = recordAtlasDic[atlasName];
}
else
{
try
{
atlasArry = Resources.LoadAll(atlasName);
recordAtlasDic.Add(atlasName, atlasArry);
}
catch
{
throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
}
}

try
{
for (int i = 0; i < atlasArry.Length; i++)
{
string key = atlasArry[i].name;
spriteDic.Add(key, atlasArry[i]);
}
spriteOntology = spriteDic[spriteName];
}
catch
{
throw new Exception(string.Format("No atlas was found:{0}", atlasName));
}
}
return spriteOntology;
}
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
 // 记录图集
private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
// 记录sprite
private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

public static Sprite LoadSprites(string atlasName, string spriteName)
{
Sprite spriteOntology = null;
spriteDic.TryGetValue(spriteName, out spriteOntology);

if (spriteOntology == null)
{
Sprite[] atlasArry = null;
recordAtlasDic.TryGetValue(atlasName, out atlasArry);
if (atlasArry != null)
{
atlasArry = recordAtlasDic[atlasName];
}
else
{
try
{
atlasArry = Resources.LoadAll(atlasName);
recordAtlasDic.Add(atlasName, atlasArry);
}
catch
{
throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
}
}

try
{
for (int i = 0; i < atlasArry.Length; i++)
{
string key = atlasArry[i].name;
spriteDic.Add(key, atlasArry[i]);
}
spriteOntology = spriteDic[spriteName];
}
catch
{
throw new Exception(string.Format("No atlas was found:{0}", atlasName));
}
}
return spriteOntology;
}