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
總結
- 上一篇: oracle 12c创建可插拔数据库(P
- 下一篇: .NET(C#):警惕PLINQ结果的无