[Unity UGUI图集系统]浅谈UGUI图集使用

**写在前面,下面是自己做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 }

三、意外情况

我在打图集的时候发现,有一张素材中间有很大一块透明区域,导致打图集时把几个尺寸比较小的素材打到这个素材中间了,使用的时候出现下面这种情况

 刚开始我以为是图集问题,不能将小尺寸打到中间有透明区域的大尺寸素材里面

后面我随手乱点,发现好了 = =||

 如果你也有类似情况,选他选他选他。。。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值