using System.Collections.Generic; using UnityEngine; [AddComponentMenu("Enviro/Seasons for Terrains")] public class EnviroTerrainSeasons : MonoBehaviour { public Terrain terrain; public bool ChangeGrass = true; public bool ChangeGrassDensity = true; public bool ChangeTextures = true; public List textureChangeIds = new List(); public Texture2D SpringTexture; public Texture2D SpringNormal; public Texture2D SummerTexture; public Texture2D SummerNormal; public Texture2D AutumnTexture; public Texture2D AutumnNormal; public Texture2D WinterTexture; public Texture2D WinterNormal; public Color SpringGrassColor; public Color SummerGrassColor; public Color AutumnGrassColor; public Color WinterGrassColor; public float SpringGrassDensity = 0.7f; public float SummerGrassDensity = 1f; public float AutumnGrassDensity = 1f; public float WinterGrassDensity; public Vector2 tiling = new Vector2(10f, 10f); private SplatPrototype[] textureInSplats = new SplatPrototype[1]; private SplatPrototype[] texturesIn; private void Start() { if (terrain == null) { terrain = GetComponent(); } texturesIn = terrain.terrainData.splatPrototypes; UpdateSeason(); EnviroSky.instance.OnSeasonChanged += delegate { UpdateSeason(); }; } private void OnEnable() { if (ChangeTextures) { if (SpringTexture == null) { Debug.LogError("Please assign a spring texture in Inspector!"); base.enabled = false; } if (SummerTexture == null) { Debug.LogError("Please assign a summer texture in Inspector!"); base.enabled = false; } if (AutumnTexture == null) { Debug.LogError("Please assign a autumn texture in Inspector!"); base.enabled = false; } if (WinterTexture == null) { Debug.LogError("Please assign a winter texture in Inspector!"); base.enabled = false; } if (textureChangeIds.Count < 1) { Debug.LogError("Please configure Texture ChangeSlot IDs!"); base.enabled = false; } } } private void ChangeGrassColor(Color ChangeToColor) { terrain.terrainData.wavingGrassTint = ChangeToColor; } private void ChangeTexture(Texture2D inTexture, int id) { textureInSplats = texturesIn; textureInSplats[id].texture = inTexture; textureInSplats[id].tileSize = tiling; terrain.terrainData.splatPrototypes = textureInSplats; terrain.Flush(); } private void ChangeTexture(Texture2D inTexture, Texture2D inNormal, int id) { textureInSplats = texturesIn; textureInSplats[id].texture = inTexture; textureInSplats[id].normalMap = inNormal; textureInSplats[id].tileSize = tiling; terrain.terrainData.splatPrototypes = textureInSplats; terrain.Flush(); } private void UpdateSeason() { switch (EnviroSky.instance.Seasons.currentSeasons) { case SeasonVariables.Seasons.Spring: { for (int k = 0; k < textureChangeIds.Count; k++) { if (ChangeTextures) { ChangeTexture(SpringTexture, textureChangeIds[k]); if (SpringNormal != null) { ChangeTexture(SpringTexture, SpringNormal, textureChangeIds[k]); } } } if (ChangeGrass) { ChangeGrassColor(SpringGrassColor); } if (ChangeGrassDensity) { terrain.detailObjectDensity = SpringGrassDensity; } break; } case SeasonVariables.Seasons.Summer: { for (int l = 0; l < textureChangeIds.Count; l++) { if (ChangeTextures) { ChangeTexture(SummerTexture, textureChangeIds[l]); if (SummerNormal != null) { ChangeTexture(SummerTexture, SummerNormal, textureChangeIds[l]); } } } if (ChangeGrass) { ChangeGrassColor(SummerGrassColor); } if (ChangeGrassDensity) { terrain.detailObjectDensity = SummerGrassDensity; } break; } case SeasonVariables.Seasons.Autumn: { for (int j = 0; j < textureChangeIds.Count; j++) { if (ChangeTextures) { ChangeTexture(AutumnTexture, textureChangeIds[j]); if (AutumnNormal != null) { ChangeTexture(AutumnTexture, AutumnNormal, textureChangeIds[j]); } } } if (ChangeGrass) { ChangeGrassColor(AutumnGrassColor); } if (ChangeGrassDensity) { terrain.detailObjectDensity = AutumnGrassDensity; } break; } case SeasonVariables.Seasons.Winter: { for (int i = 0; i < textureChangeIds.Count; i++) { if (ChangeTextures) { ChangeTexture(WinterTexture, textureChangeIds[i]); if (WinterNormal != null) { ChangeTexture(WinterTexture, WinterNormal, textureChangeIds[i]); } } } if (ChangeGrass) { ChangeGrassColor(WinterGrassColor); } if (ChangeGrassDensity) { terrain.detailObjectDensity = WinterGrassDensity; } break; } } } }