**寫在前面,下面是自己做Demo的時候一些記錄吧,參考了很多網上分享的資源
一、打圖集
1.准備好素材(建議最好是根據圖集名稱按文件夾分開)
2、創建一個SpriteAtlas
3、將素材添加到圖集中
4、生成圖集
到此,我們的圖集就准備好了
二、加載圖集
1、在工程里面使用(正常包內使用建議打成AB,更新比較方便,加載方式和下面一樣,工程為了方便,我將上面打好的圖集放在Resources下面)
2、這是最喜歡的c+v環節,加載圖集

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.U2D; 5 6 public class UIResourceLoadManager : UnitySingleton<UIResourceLoadManager> 7 { 8 9 private Dictionary<string, SpriteAtlas> mUISpriteAtlasDic = new Dictionary<string, SpriteAtlas>(); 10 11 private T LoadResouceOfType<T>(string _resPath) where T:Object 12 { 13 T tempResource = null; 14 tempResource = Resources.Load<T>(_resPath); 15 return tempResource; 16 } 17 18 public SpriteAtlas GetSpriteAtlas(string _atlasName) 19 { 20 if (mUISpriteAtlasDic.ContainsKey(_atlasName)) 21 { 22 if (mUISpriteAtlasDic[_atlasName] == null) mUISpriteAtlasDic[_atlasName] = LoadResouceOfType<SpriteAtlas>("Chart/"+_atlasName); 23 } 24 else 25 { 26 mUISpriteAtlasDic.Add(_atlasName, LoadResouceOfType<SpriteAtlas>("Chart/" + _atlasName)); 27 } 28 return mUISpriteAtlasDic[_atlasName]; 29 } 30 31 public Sprite LoadSprite(string _atlasName,string _spriteName) 32 { 33 Sprite tempSprite = null; 34 SpriteAtlas tempAtlas = GetSpriteAtlas(_atlasName); 35 if(tempAtlas != null ) tempSprite = tempAtlas.GetSprite(_spriteName); 36 return tempSprite; 37 } 38 39 public Sprite[] LoadSprites(string _atlasName, Sprite[] _spriteArray) 40 { 41 SpriteAtlas tempAtlas = GetSpriteAtlas(_atlasName); 42 if (tempAtlas != null) 43 { 44 if (_spriteArray == null || _spriteArray.Length < tempAtlas.spriteCount) _spriteArray = new Sprite[tempAtlas.spriteCount]; 45 if (tempAtlas != null) tempAtlas.GetSprites(_spriteArray); 46 } 47 return _spriteArray; 48 } 49 } 50 51 public class UnitySingleton<T> : MonoBehaviour where T : Component 52 { 53 private static T _instance; 54 public static T Instance 55 { 56 get 57 { 58 if (_instance == null) 59 { 60 _instance = FindObjectOfType(typeof(T)) as T; 61 if (_instance == null) 62 { 63 GameObject tempObject = new GameObject(); 64 tempObject.hideFlags = HideFlags.HideAndDontSave; 65 _instance = (T)tempObject.AddComponent(typeof(T)); 66 Object.DontDestroyOnLoad(tempObject); 67 } 68 } 69 return _instance; 70 } 71 } 72 }
3、使用舉例

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class UIAtlasLoadImag : MonoBehaviour 7 { 8 public Image image; 9 // Start is called before the first frame update 10 void Start() 11 { 12 if (image) image.sprite = UIResourceLoadManager.Instance.LoadSprite("icon","小地圖底"); 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 19 } 20 }
三、意外情況
我在打圖集的時候發現,有一張素材中間有很大一塊透明區域,導致打圖集時把幾個尺寸比較小的素材打到這個素材中間了,使用的時候出現下面這種情況
剛開始我以為是圖集問題,不能將小尺寸打到中間有透明區域的大尺寸素材里面
后面我隨手亂點,發現好了 = =||
如果你也有類似情況,選他選他選他。。。
---------------------------------------------------------------------------------------------------------------------------------------------------
寫在后面,上面素材馬賽克是為了避免產生一些不必要的麻煩。
我的工作是使用NGUI項目,用的是5.X版本Unity,最近想用用新版本Unity,一下從5.x到2019,有種從石器時代到工業時代的趕腳,新奇,哈哈
很久不記錄這個東西了,這次因為想試試看把自己用NGUI實現的內容完全轉換成UGUI,記錄一下過程,和一些差異實現,開發日記之類的