Unity运行时检测Altas使用情况

2023-05-10,,

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));
}
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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

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

Unity运行时检测Altas使用情况的相关教程结束。

《Unity运行时检测Altas使用情况.doc》

下载本文的Word格式文档,以方便收藏与打印。