It seems there is not any built-in function to do what you want, but you can write your own function to do what you wantfind every object in a given layer. However, be careful, the function is quite slow, so do not use it frequently :
using UnityEngine ;
using System.Collections.Generic ;
private class PlayersManagerByLayer
{
private Dictionary<int,List<GameObject>> gameObjectsByLayer ;
public PlayersManagerByLayer()
{
gameObjectsByLayer = new Dictionary<int,List<GameObject>>();
}
public void AddPlayer( GameObject player )
{
if( !gameObjectsByLayer.ContainsKey( player.layer ) )
gameObjectsByLayer.Add( player.layer, new List<GameObject>() );
if( !gameObjectsByLayer[player.layer].Contains(player ) )
gameObjectsByLayer[player.layer].Add( player ) ;
}
public void RemovePlayer( GameObject player )
{
if( gameObjectsByLayer.ContainsKey( player.layer ) && gameObjectsByLayer[player.layer].Contains(player ) )
gameObjectsByLayer[player.layer].Remove( player ) ;
}
public void MovePlayerToLayer( GameObject player, int newLayer )
{
RemovePlayer( player ) ;
player.layer = newLayer ;
AddPlayer( player ) ;
}
public List<GameObject> GetPlayersFromLayer( int layer )
{
if( !gameObjectsByLayer.ContainsKey( layer ) )
return null ;
else
return gameObjectsByLayer[layer] ;
}
}