Hello, I have a Terrain with a few TerrainLayers and I would like to change the tint color of the TerrainLayers via script.
The tint color property is only visible through the Terrain inspector and not through the TerrainLayer inspector and likewise, I don’t see a colorTint property in the Visual Studio suggestions under terrainLayer. Interestingly though, changing the color tint changes the color for all terrains that use that TerrainLayer.
Does anybody know if it is possible to change the TerrainLayer color tint via script?
If not, the only alternative I see would be to create new textures with the color already baked in and apply those via script using the terrainLayer.diffuseTexture property.
How to change via code is answered above, but here’s how to change via the inspector:
Select TerrainLayer - Inspector > Debug mode in top-right. Adjust Diffuse Remap Max (no color picker).
Here’s a script to change it via the inspector.
using System.Collections.Generic;
using UnityEngine;
// https://gist.github.com/st4rdog/40fc14a8a256c2376ba121bca33d7571
// Gets TerrainLayers assets used on selected terrain and applies tint/color.
// TerrainLayer color can be changed in Debug inspector view.
public class TerrainColorTint : MonoBehaviour
{
public List<Terrain> Terrains = new();
public List<Color> TargetColors = new();
public TerrainLayer TerrainLayer;
void OnValidate()
{
if (Terrains == null || Terrains.Count == 0) return;
var layersCount = Terrains[0].terrainData.terrainLayers.Length;
// Init with current colours
{
if (TargetColors.Count != layersCount)
{
TargetColors.Clear();
for (int i = 0; i < layersCount; i++)
{
TargetColors.Add(Terrains[0].terrainData.terrainLayers[i].diffuseRemapMax);
}
}
}
// Apply
{
for (int i = 0; i < Terrains.Count; i++)
{
var terrain = Terrains[i];
if (terrain == null) return;
for (int j = 0; j < layersCount; j++)
{
terrain.terrainData.terrainLayers[j].diffuseRemapMax = TargetColors[j];
}
}
}
}
}