I have an editor script generating a mesh, applying a material to that mesh, and updating some values on the material to pass on to a custom shader (The shader basically displays different colors according to mesh height)
My problem is that the changes made on the material aren’t saved. If I save the scene where my mesh is generated, the materials values are lost.
Here is a code snippet executed in my editor scripts that sets the materials values :
("material " is a public variable, set in unity inspector)
material.SetInt("baseColorCount", baseColors.Length);
material.SetColorArray("baseColors", baseColors);
material.SetFloatArray("baseStartHeights", baseStartHeights);
material.SetFloatArray("baseBlends", baseBlends);
meshGameObject.GetComponent<MeshRenderer>().sharedMaterial = material;
And here is the shader :
Shader "Custom/TerrainShader"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
const static int maxColorCount = 10;
// To avoid potential div / 0 problems in inverseLerp
const static float epsilon = 1E-4;
int baseColorCount;
float3 baseColors[maxColorCount];
float baseStartHeights[maxColorCount];
float baseBlends[maxColorCount];
float minHeight;
float maxHeight;
struct Input
{
float3 worldPos;
};
float inverseLerp(float a, float b, float value) {
return saturate((value-a)/(b-a));
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
float heightPercent = inverseLerp(minHeight, maxHeight, IN.worldPos.y);
for (int i = 0; i < baseColorCount; i++)
{
float drawStrength = inverseLerp(-baseBlends<em>/2 - epsilon, baseBlends_/2 - epsilon, heightPercent - baseStartHeights*);*_</em>
o.Albedo = o.Albedo * (1 - drawStrength) + baseColors * drawStrength;
}
}
ENDCG
}
FallBack “Diffuse”
}
Now after my code is executed, everything is ok :
[159463-material-ok.png|159463]
But if I save the scene for example, the material loses its values :
[159464-material-ko.png*|159464]*
*
*
I tried loading the material from AssetsDatabase / Saving it with SaveAssets() but that didn’t change anything.
I feel like I’m missing something but I can’t figure it out.
What should I do so that the changes I made on a material in my editor script are actually saved on the material ?