1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 //纹理图集加载管理
5 public class PPTextureManage : MonoBehaviour {
6 private static GameObject m_pMainObject;
7 private static PPTextureManage m_pContainer =
null;
8 public static PPTextureManage getInstance(){
9 if(m_pContainer ==
null){
10 m_pContainer = m_pMainObject.GetComponent<PPTextureManage>
();
11 }
12 return m_pContainer;
13 }
14 private Dictionary<
string, Object[]> m_pAtlasDic;
//图集的集合
15 void Awake(){
16 initData ();
17 }
18 private void initData(){
19 PPTextureManage.m_pMainObject =
gameObject;
20 m_pAtlasDic =
new Dictionary<
string, Object[]>
();
21 }
22 // Use this for initialization
23 void Start () {
24 }
25 //加载图集上的一个精灵
26 public Sprite LoadAtlasSprite(
string _spriteAtlasPath,
string _spriteName){
27 Sprite _sprite =
FindSpriteFormBuffer (_spriteAtlasPath,_spriteName);
28 if (_sprite ==
null) {
29 Object[] _atlas =
Resources.LoadAll (_spriteAtlasPath);
30 m_pAtlasDic.Add (_spriteAtlasPath,_atlas);
31 _sprite =
SpriteFormAtlas (_atlas,_spriteName);
32 }
33 return _sprite;
34 }
35 //删除图集缓存
36 public void DeleteAtlas(
string _spriteAtlasPath){
37 if (m_pAtlasDic.ContainsKey (_spriteAtlasPath)) {
38 m_pAtlasDic.Remove (_spriteAtlasPath);
39 }
40 }
41 //从缓存中查找图集,并找出sprite
42 private Sprite FindSpriteFormBuffer(
string _spriteAtlasPath,
string _spriteName){
43 if (m_pAtlasDic.ContainsKey (_spriteAtlasPath)) {
44 Object[] _atlas =
m_pAtlasDic[_spriteAtlasPath];
45 Sprite _sprite =
SpriteFormAtlas(_atlas,_spriteName);
46 return _sprite;
47 }
48 return null;
49 }
50 //从图集中,并找出sprite
51 private Sprite SpriteFormAtlas(Object[] _atlas,
string _spriteName){
52 for (
int i =
0; i < _atlas.Length; i++
) {
53 if (_atlas [i].GetType () ==
typeof(UnityEngine.Sprite)) {
54 if(_atlas [i].name ==
_spriteName){
55 return (Sprite)_atlas [i];
56 }
57 }
58 }
59 Debug.LogWarning (
"图片名:"+_spriteName+
";在图集中找不到");
60 return null;
61 }
62 }
转载于:https://www.cnblogs.com/LLWH134/p/8176369.html
相关资源:Simple Sprite Packer UGUI图集打包工具