# MergeOntoThis

### Introduction

MergeOntoThis can be used to merge tiles from one or more *source* layers into a *destination* layer. Merging can be used for a number of reasons:

1. To simplify management of layers
2. To improve performance

### Example - Merging Two Layers Into One

This example uses a tmx file which includes a layer named Layer2 which contains ice blocks.

![](/files/lmoThHjiy54VP8fCFN1J)

The other tiles (the red number 1 and the orange brick surrounding blocks) are part of the GameplayLayer - the standard layout for Level1 if using the wizard. The following code shows how to merge the layers:

```
void CustomInitialize()
{
    var gameplayLayer = Map.MapLayers.FindByName("GameplayLayer");
    var quadCountBefore = gameplayLayer.QuadCount;
    var layerCountBefore = Map.MapLayers.Count;

    var layer2 = Map.MapLayers.FindByName("Layer2");

    gameplayLayer.MergeOntoThis(new List { layer2 });

    layer2.Destroy();
    var quadCountAfter = gameplayLayer.QuadCount;
    var layerCountAfter = Map.MapLayers.Count;
}
```

The code above includes diagnostic variables to count quads and layers. These values can help verify that the layers have been merged at runtime as shown in the following screenshot:

![](/files/SmcMwLW9N8tChGgJ5rbx)

Even though layer2 has been destroyed, the ice blocks still draw as shown in the following screenshot:

![](/files/gex3ioa0DCx090zAGcRZ)

### Merging and Partitioning

Note that LayeredTileMaps partition their tiles based on the X or Y axis, so merging is not simply an addition of quads at the end of the layer. Rather, merging inserts the quads to preserve the axis sorting so that efficient partitioning during drawing can be performed.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/tiled-plugin/flatredball-tilegraphics-mapdrawablebatch/mergeontothis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
