UGUI图集管理

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值