The question is answered, right answer was accepted
How do I edit the terrainData.detailPrototype.healthyColor at runtime?
Hello everyone!
I'd like the player to be able to modify the color of a furry carpet, using a GUI slider. So far the player can change the material.color of textures, but when it comes to grass, it seems less straight forward. I've read different posts...
Assigning the color directly, as in my code, doesn't work.
TerrainData.RefreshPrototypes doesn't help.
And SetDetailLayer, as from the link below, seems to be about editing the number of details?
http://answers.unity3d.com/answers/13398/view.html
And the following link doesn't explain terrain in detail:
http://answers.unity3d.com/questions/13854/edit-terrain-foliagetexture-at-runtime.html
Here's my code:
private DetailPrototype _detailPrototype;
void Start()
{
_detailPrototype = _terrain.terrainData.detailPrototypes[0];
}
void Update()
{
_detailPrototype.healthyColor = Color.HSVToRGB(cSliderValueCarpet / 255f, hSliderValueCarpet / 255f, 1f);
}
Answer by Knarhoi · Jul 17, 2016 at 09:02 PM
I found the solution!
Basically you have to assign to the prototype array itself and not to the individual prototypes.
private DetailPrototype[] _detailPrototypes;
void Start()
{
_detailPrototypes = _terrain.terrainData.detailPrototypes;
}
void Update()
{
_detailPrototypes[0].healthyColor = Color.HSVToRGB(cSliderValueCarpet / 255f, hSliderValueCarpet / 255f, 1f);
_terrain.terrainData.detailPrototypes = _detailPrototypes;
}
What about this one? , Grass color is not getting chnaged...what to do??
public class Grass_Color_Changer : MonoBehaviour { public Color Grass_Color;
TerrainData terrainData;
public void Start()
{
terrainData = GetComponent<Terrain>().terrainData;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
terrainData.detailPrototypes[0].dryColor = Color.HSVToRGB(255f, 255f, 1f);
terrainData.detailPrototypes[0].healthyColor = Color.HSVToRGB(255f, 225f, 1f);
Debug.Log("Working");
}
}
It looks like you're providing the same color (255f, 255f, 1f) as both dryColor and healthyColor. So you won't be able to tell the difference, if that's what you're going for?
If that was intentional, try the link I put in my answer, it describes different scenarios for updating trees and grass data.
Follow this Question
Related Questions
Grass is aliased on one terrain but isn't on the other 0 Answers
Grass detail doesn't work 0 Answers
Auto grass placement not working properly 1 Answer
Interactive Grass with terrain tool 0 Answers
Terrain : Detail paint isn't working on Unity 2018.2.0b11 2 Answers