当前位置:   article > 正文

Unity 图集中加载图片_unity 加载图集

unity 加载图集
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class SpriteAtlasUtility : MonoBehaviour
{
    public static SpriteAtlasUtility Instance;
    private Dictionary<string , Sprite> spriteDic;
    private Dictionary<string , SpriteAtlas> atlasDic;
    private void Awake() {
        Instance = this;
        spriteDic = new Dictionary<string , Sprite>();
        atlasDic = new Dictionary<string , SpriteAtlas>();
    }

    public Sprite GetAtlasSprite(string atlasName,string spriteName) {
        if (spriteDic.TryGetValue(spriteName,out Sprite sprite)) {
            return sprite;
        }
        sprite = GetSpriteAtlas(atlasName)?.GetSprite(spriteName);
        spriteDic [spriteName] = sprite;
        return sprite;
    }
    public Sprite GetAtlasSprite(string spriteName) {
        if (spriteDic.TryGetValue(spriteName,out Sprite sprite)) {
            return sprite;
        }
        return sprite;
    }

    private SpriteAtlas GetSpriteAtlas(string atlasName) {
        SpriteAtlas atlas;
        if (!atlasDic.ContainsKey(atlasName)) {
            //TODO:AB或者Resources加载图集
            atlas = null;
        }
        else {
            atlas = atlasDic [atlasName];
        }
        return atlas;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/103501
推荐阅读
相关标签
  

闽ICP备14008679号