Territories from a Color Map
beginner tasksTerrain Grid System 2 · Common Tasks
Territories from a Color Map
You can create the territories of a grid from an image in which each territory is painted with its own color. Terrain Grid System 2 reads the color under the center of each cell and creates one territory for each different color. The demo scene Demo13_TerritoryByTexture shows this feature.
Prepare the Image
- Paint each territory with one solid color. Do not use antialiasing, gradients or soft brushes, because each different color creates a new territory.
- Paint the area that must not become a territory, such as the sea, with one color. You will use this color as the Neutral Color.
- The image is stretched over the whole grid. Up to 512 different colors are supported, including the neutral color.
Texture Import Settings
Select the image in the Project window and use these import settings. Other values change the colors of the image and create many small territories along the borders.
| Setting | Value |
|---|---|
| Read/Write (Advanced) | Enabled. The asset reads the pixels of the image, also in builds. |
| Non-Power of 2 (Advanced) | None. |
| Max Size | Equal to or larger than the width and the height of the image. |
| Compression | None. |
If you use platform overrides for this image, use the same values in each override.
Create the Territories in the Inspector
- Select the Terrain Grid System object and open the Grid Configuration section.
- Set Territories to any value above 0. The Territories Texture field appears.
- Assign your image to Territories Texture.
- Set Neutral Color to the color of the area that must be ignored. Type its hexadecimal value in the color picker, because the eyedropper can return a slightly different color.
- Enable Hide Neutral Cells if the cells of the neutral area must be invisible.
- Click Generate Territories. Click it again after you change the image or the neutral color.
The number of territories comes from the colors of the image, so the Territories value only turns the feature on. Each cell takes the color under its center, so a grid with more cells follows the borders of the image more closely. To show the territories with the colors of the image, enable Colorize Territories in the Territories Appearance section.
The territories are created again from the image each time the grid is generated, also when the scene starts. Keep the image assigned to the grid.
Create the Territories from Code
The following script creates the territories from a map when the scene starts, then gives a name to the territory painted in red. Copy it into a new file named TerritoriesFromMap.cs, add it to any object and assign the grid and the map in the inspector. Replace the neutral color and the red color with the colors of your map.
using UnityEngine;
using TGS;
public class TerritoriesFromMap : MonoBehaviour {
public TerrainGridSystem tgs;
public Texture2D map;
public Color32 neutralColor = new Color32(20, 60, 140, 255);
void Start () {
// Create one territory per color of the map
tgs.numTerritories = 1;
tgs.territoriesTexture = map;
tgs.territoriesTextureNeutralColor = neutralColor;
tgs.territoriesHideNeutralCells = true;
tgs.GenerateMap();
Debug.Log("Territories created: " + tgs.numTerritories);
// Name the territory painted in red on the map
int index = GetTerritoryIndex(new Color32(200, 40, 40, 255));
if (index >= 0) {
tgs.territories[index].name = "Red Kingdom";
}
}
// Returns the territory created from a map color, or -1
int GetTerritoryIndex (Color32 mapColor) {
for (int k = 0; k < tgs.territories.Count; k++) {
Color32 c = tgs.territories[k].fillColor;
if (c.r == mapColor.r && c.g == mapColor.g && c.b == mapColor.b) {
return k;
}
}
return -1;
}
}
Each territory created from the image keeps the color of the image in fillColor, so you can find the territory of any color as GetTerritoryIndex does above. For other territory methods, see the Territories API.
Troubleshooting
| Problem | Solution |
|---|---|
| Many small territories appear along the borders. | The import settings or the image changed the colors. Check the import settings above and make sure the image has no antialiasing. |
| The neutral area is still visible. | Make sure Hide Neutral Cells is enabled and that Neutral Color has exactly the same values as the color in the image. If it is still visible, paint the neutral area pure black (000000) or pure white (FFFFFF) and use that color. |
| The console shows "Texture used to create territories is not readable". | Enable Read/Write in the import settings of the image. |
Suggest an improvement
Help us improve this documentation page.