zoukankan      html  css  js  c++  java
  • [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 }
    View Code

    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 }
    View Code

    三、意外情况

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

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

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

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

    ---------------------------------------------------------------------------------------------------------------------------------------------------

    写在后面,上面素材马赛克是为了避免产生一些不必要的麻烦。

    我的工作是使用NGUI项目,用的是5.X版本Unity,最近想用用新版本Unity,一下从5.x到2019,有种从石器时代到工业时代的赶脚,新奇,哈哈

    很久不记录这个东西了,这次因为想试试看把自己用NGUI实现的内容完全转换成UGUI,记录一下过程,和一些差异实现,开发日记之类的

  • 相关阅读:
    leetcode刷题-54螺旋矩阵
    leetcode刷题-53最大子序和
    leetcode刷题-52N皇后2
    leetcode刷题-51N皇后
    leetcode刷题-50Pow(x, n)
    leetcode刷题-37解数独
    leetcode刷题-49字母异位词分组
    leetcode刷题-48旋转图像
    数据结构—B树、B+树、B*树
    LeetCode–旋转数组的最小数字
  • 原文地址:https://www.cnblogs.com/lovewaits/p/12895129.html
Copyright © 2011-2022 走看看