UGUI获取图集工具类

发布于 2021-07-28  118 次阅读


UGUI获取图集工具类

因为NGUI内部已经封装了方法,获取图集比较容易,但是UGUI需要自己来封装,所以动态设置图集的时候就比较麻烦,所以简单写了一个方法,这里记录一下以后直接拿来用就可以了:

    // 记录图集
   private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
    // 记录sprite 
   private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

    public static Sprite LoadSprites(string atlasName, string spriteName)
    {
        Sprite spriteOntology = null;
        spriteDic.TryGetValue(spriteName, out spriteOntology);

        if (spriteOntology == null)
        {
            Sprite[] atlasArry = null;
            recordAtlasDic.TryGetValue(atlasName, out atlasArry);
            if (atlasArry != null)
            {
                atlasArry = recordAtlasDic[atlasName];
            }
            else
            {
                try
                {
                    atlasArry = Resources.LoadAll(atlasName);
                    recordAtlasDic.Add(atlasName, atlasArry);
                }
                catch
                {
                    throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
                }
            }

            try
            {
                for (int i = 0; i < atlasArry.Length; i++)
                {
                    string key = atlasArry[i].name;
                    spriteDic.Add(key, atlasArry[i]);
                }
                spriteOntology = spriteDic[spriteName];
            }
            catch
            {
                throw new Exception(string.Format("No atlas was found:{0}", atlasName));
            }
        }
        return spriteOntology;
    }
    // 记录图集
   private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
    // 记录sprite 
   private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

    public static Sprite LoadSprites(string atlasName, string spriteName)
    {
        Sprite spriteOntology = null;
        spriteDic.TryGetValue(spriteName, out spriteOntology);

        if (spriteOntology == null)
        {
            Sprite[] atlasArry = null;
            recordAtlasDic.TryGetValue(atlasName, out atlasArry);
            if (atlasArry != null)
            {
                atlasArry = recordAtlasDic[atlasName];
            }
            else
            {
                try
                {
                    atlasArry = Resources.LoadAll(atlasName);
                    recordAtlasDic.Add(atlasName, atlasArry);
                }
                catch
                {
                    throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
                }
            }

            try
            {
                for (int i = 0; i < atlasArry.Length; i++)
                {
                    string key = atlasArry[i].name;
                    spriteDic.Add(key, atlasArry[i]);
                }
                spriteOntology = spriteDic[spriteName];
            }
            catch
            {
                throw new Exception(string.Format("No atlas was found:{0}", atlasName));
            }
        }
        return spriteOntology;
    }
    // 记录图集
   private static Dictionary<string, Sprite[]> recordAtlasDic = new Dictionary<string, Sprite[]>();
    // 记录sprite 
   private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

    public static Sprite LoadSprites(string atlasName, string spriteName)
    {
        Sprite spriteOntology = null;
        spriteDic.TryGetValue(spriteName, out spriteOntology);

        if (spriteOntology == null)
        {
            Sprite[] atlasArry = null;
            recordAtlasDic.TryGetValue(atlasName, out atlasArry);
            if (atlasArry != null)
            {
                atlasArry = recordAtlasDic[atlasName];
            }
            else
            {
                try
                {
                    atlasArry = Resources.LoadAll(atlasName);
                    recordAtlasDic.Add(atlasName, atlasArry);
                }
                catch
                {
                    throw new Exception(string.Format("Resources.Load{0}fail!", atlasName));
                }
            }

            try
            {
                for (int i = 0; i < atlasArry.Length; i++)
                {
                    string key = atlasArry[i].name;
                    spriteDic.Add(key, atlasArry[i]);
                }
                spriteOntology = spriteDic[spriteName];
            }
            catch
            {
                throw new Exception(string.Format("No atlas was found:{0}", atlasName));
            }
        }
        return spriteOntology;
    }

"You got to put the past behind you before you can move on. "