- Home /
Know if layer is in use
I have a bunch of layers in a large project, some of which i'm not sure if they can be deleted or not, is it possible to know if those layers are being used somewhere in the project?
you'd need to write a script that runs through the project folder, each scene and within each scene through each transform. if you then OR each layer mask on top of each other and finally check the bits you'll know which ones stay 0, meaning are not used.
I was hoping there would be an easier way, if I decide to write the script i'll post it here as an answer, thanks.
Answer by Cambesa · Feb 12, 2020 at 11:46 AM
If anyone is interested, here's a piece of code that logs all unused layers. You have to execute this per scene though.
public static void CheckUsedLayers()
{
Debug.Log("Check used layers");
Dictionary<string, int> layerCount = new Dictionary<string, int>();
List<GameObject> gameObjects = FindObjectsOfType<GameObject>().ToList();
// iterate objects and save to dictionary
for (int i = 0; i < gameObjects.Count; ++i)
{
string layerName = LayerMask.LayerToName(gameObjects[i].layer);
if (layerCount.ContainsKey(layerName))
{
layerCount[layerName]++;
}
else
{
layerCount.Add(layerName, 1);
}
}
// log to console
foreach (KeyValuePair<string, int> entry in layerCount)
{
Debug.Log(entry.Key + ": " + entry.Value);
}
// unused layers
List<string> layerNames = new List<string>();
for (int i = 8; i <= 31; i++) //user defined layers start with layer 8 and unity supports 31 layers
{
var layerN = LayerMask.LayerToName(i); //get the name of the layer
if (layerN.Length > 0) //only add the layer if it has been named (comment this line out if you want every layer)
layerNames.Add(layerN);
}
List<string> listOfKeys = layerCount.Keys.ToList();
List<string> unusedLayers = layerNames.Except(listOfKeys).ToList();
string joined = string.Join(", ", unusedLayers);
Scene scene = SceneManager.GetActiveScene();
Debug.Log("Unused layers in " + scene.name + ": " + joined);
Debug.Log("Check used layers done");
}
While this is certainly useful, keep in $$anonymous$$d that you could have prefabs which have a different layer assigned. So you might are missing those.