/// <summary> /// Retrieves a list of prefabs that belong to a layer. /// </summary> /// <param name="map"> /// The reference to a <see cref="GridMap"/> type. /// </param> /// <param name="layerIndex"> /// The index of the layer that will have it's prefabs returned. /// </param> /// <returns> /// Returns a <see cref="List{T}"/> containing the game objects that exist on the specified layer. /// </returns> public static List <GameObject> GetLayerPrefabs(this GridMap map, int layerIndex) { // find list of objects to delete var items = new List <GameObject>(); for (var col = 0; col < map.Columns; col++) { for (var row = 0; row < map.Rows; row++) { var obj = map.SearchForExistingPrefab(layerIndex, col, row); if (obj != null) { items.Add(obj); } } } return(items); }
/// <summary> /// Finds and renames prefabs so that they are on different layers /// </summary> /// <param name="map">The reference to a <see cref="GridMap"/> type.</param> /// <param name="index">The index of the layer.</param> /// <param name="newIndex">The index of the other layer.</param> /// <param name="callback">A callback that will be called with the map, and game objects that will be affected by the swap.</param> /// <param name="completedCallback">A callback that will be called with the map, and game objects that were affected by the swap.</param> /// <remarks>The <see cref="callback"/> and <see cref="completedCallback"/> parameters will be called with the fallowing arguments (Map, LayerIndex, NewLayerIndex, LayerA, LayerB).</remarks> public static void SwapLayers(this GridMap map, int index, int newIndex, Action <SwapModel> callback, Action <SwapModel> completedCallback) { // find list of objects to delete var layerA = new List <GameObject>(); var layerB = new List <GameObject>(); for (var col = 0; col < map.Columns; col++) { for (var row = 0; row < map.Rows; row++) { var obj = map.SearchForExistingPrefab(index, col, row); if (obj != null) { layerA.Add(obj); } obj = map.SearchForExistingPrefab(newIndex, col, row); if (obj != null) { layerB.Add(obj); } } } var layerAObjects = layerA.ToArray(); var layerBObjects = layerB.ToArray(); var swapModel = new SwapModel() { Map = map, LayerIndex = index, NewLayerIndex = newIndex, LayerAObjects = layerAObjects, LayerBObjects = layerBObjects }; if (callback != null) { callback(swapModel); } // setup variables const string NameFormat = "{0}_l{1}_c{2}_r{3}"; // setup processor callback var changeNames = new Action <List <GameObject>, int>( (stack, layerIndex) => { foreach (var item in stack) { string name; int layer; int column; int row; if (!item.name.TryParsePrefabName(out name, out layer, out column, out row)) { continue; } // give the prefab a name that represents it's location within the grid map item.name = string.Format(NameFormat, name, layerIndex, column, row); // move prefab into proper layer position var transform = item.transform; var position = transform.localPosition; position.y = layerIndex * map.Depth; transform.position = position; } }); // change names changeNames(layerA, newIndex); changeNames(layerB, index); // change model indexes var model = map.Layers[index]; map.Layers[index] = map.Layers[newIndex]; map.Layers[newIndex] = model; if (completedCallback != null) { completedCallback(swapModel); } }