Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Last active May 6, 2024 03:04
Show Gist options
  • Save st4rdog/40fc14a8a256c2376ba121bca33d7571 to your computer and use it in GitHub Desktop.
Save st4rdog/40fc14a8a256c2376ba121bca33d7571 to your computer and use it in GitHub Desktop.
Unity Terrain - Tint TerrainLayers. Gets TerrainLayers assets used on selected terrain and applies tint/color.
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();
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];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment