Unity 切割图集并获取图集

原文地址,多些大佬:unity切割图集并且读取图集

 

 

public class SpriteManager : MonoBehaviour
{

    private static GameObject spriteManager;
    private static SpriteManager instance = null;

    public static SpriteManager getInstance()
    {
        if(instance == null)
        {
            instance = spriteManager.GetComponent<SpriteManager>();
        }
        return instance;
    }

    private static Dictionary<string, UnityEngine.Object[]> mAtlasDic;//图集的集合

    void Awake()
    {
        SpriteManager.spriteManager = gameObject;
        init();
    }

    public void init()
    {
        mAtlasDic = new Dictionary<string, UnityEngine.Object[]>();
    }

    public Sprite LoadAtlasSprite(string path, string _spriteName)
    {
        Sprite _sprite = FindSpriteFormBuffer(path, _spriteName);
        if(_sprite == null)
        {
            UnityEngine.Object[] _atlas = Resources.LoadAll(path);
            mAtlasDic.Add(path, _atlas);
            _sprite = SpriteFormAtlas(_atlas, _spriteName);
        }
        return _sprite;
    }

    public void DeleteAtlas(string path)
    {
        if (mAtlasDic.ContainsKey(path))
        {
            mAtlasDic.Remove(path);
        }
    }

    private Sprite FindSpriteFormBuffer(string path, string _spriteName)
    {
        if (mAtlasDic.ContainsKey(path))
        {
            UnityEngine.Object[] _atlas = mAtlasDic[path];
            Sprite _sprite = SpriteFormAtlas(_atlas, _spriteName);
            return _sprite;
        }
        return null;
    }

    private Sprite SpriteFormAtlas(UnityEngine.Object[] _atlas, string _spriteName)
    {
        for (int i = 0; i < _atlas.Length; i++)
        {
            if (_atlas[i].GetType() == typeof(UnityEngine.Sprite))
            {
                if (_atlas[i].name == _spriteName)
                {
                    return (Sprite)_atlas[i];
                }
            }
        }
        return null;
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值