Unity运行时检测Altas使用情况

UI贴图在游戏中内存大小中占的分量非常非常大,尤其对于前期对UI没有规划的项目,无论是包量还是内存大小都是需要花费很多时间去优化。如果涉及到战斗场景和逻辑场景的情况下,常用的做法就是把两个场景使用的atlas严格的分离开,这样可以减少运行时内存,特别是在战斗中,内存增加的比较厉害。OK,如果项目前期这方面的事情考虑比较周全、规则比较详细、执行也比较到位,后期可能就做这个事情就比较简答。那如果出现战斗中引用不该有的atlas怎么办?UI太多的情况下,逐个排除太麻烦,尤其是不在UI中,只是静态引进的就更头疼。下面的代码就是检测atlas使用情况。UGUI默认打包后会把名字前缀”SpriteAtlasTexture”

  void CheckUsedAtlas(string atlasName)
    {
        Image[] gos = Resources.FindObjectsOfTypeAll(typeof(Image)) as Image[];
        for (int i = 0; i < gos.Length; i++)
        {
            if (EditorUtility.IsPersistent(gos[i].transform.root.gameObject))
                continue;

            if (gos[i] == null)
                continue;

            if (gos[i].mainTexture.name.Contains("SpriteAtlasTexture-"+atlasName))
            {
                string name = gos[i].name;
                Transform parent = gos[i].transform.parent;
                while (parent != null)
                {
                    name = parent.name + "/" + name;
                    parent = parent.parent;
                }
                BWDebug.LogError(string.Format("Use {0}:{1}", atlasName,name));
            }
        }
    }

网上找到另外一种检测方式,检测的范围会更广泛一些,使用Texture中返回的指针接口来进行判断,可以坐下参考

http://www.iverv.com/2015/04/unity-scriptspritepackeratlassprite.html

原文地址:https://www.cnblogs.com/zsb517/p/5787286.html