I wrote a custom Editor script for a class of mine to set colors to a material whenever any of the color values change, this gives me the option to preview the colors on the go.
The script works very well but when I save the scene there is something weird happening. All the colors change to some weird defaults and stay that way until I click on the object itself, then it gets updated to the correct colors that I chose before.
This is what the sprite looks like before I save the scene:
And here's what it looks like after I save the scene:
As I've mentioned, after I click on the GameObject, the sprite updates to previously saved colors.
Here's the code for the custom Editor script:
public class PaletteEditor : Editor
{
SerializedProperty Col1,
Col2,
Col3,
Col4;
public void OnEnable()
{
Col1 = serializedObject.FindProperty("Col1");
Col2 = serializedObject.FindProperty("Col2");
Col3 = serializedObject.FindProperty("Col3");
Col4 = serializedObject.FindProperty("Col4");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
Col1.colorValue = EditorGUILayout.ColorField("Col1", Col1.colorValue);
Col2.colorValue = EditorGUILayout.ColorField("Col2", Col2.colorValue);
Col3.colorValue = EditorGUILayout.ColorField("Col3", Col3.colorValue);
Col4.colorValue = EditorGUILayout.ColorField("Col4", Col4.colorValue);
((ColorMatrixPicker)target).SetColorMatrix();
serializedObject.ApplyModifiedProperties();
}
}
EDIT:
I've tried SonicBlue22's solution but the problem still persists. I've fiddled around with the script and I think the problem might be with the shader itself as it does not keep the values assigned to it by the script.
Here's the shader code:
Shader "Custom/PaletteSwap" {
Properties{
_Color("Color", Color) = (1,1,1,1)
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.5
_BumpTex("Bump map", 2D) = "bump" {}
}
SubShader
{
Tags
{
"Queue" = "Geometry"
"RenderType" = "TransparentCutout"
}
Cull Off
CGPROGRAM
#pragma surface surf Lambert addshadow fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BumpTex;
fixed4 _Color;
fixed _Cutoff;
half4x4 _ColorMatrix;
struct Input
{
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_MainTex));
o.Albedo = _ColorMatrix[c.r * 3];
o.Alpha = c.a;
clip(o.Alpha - _Cutoff);
}
ENDCG
}
FallBack "Diffuse"
}
I assign the color values straight to the matrix like so:
Mat.SetMatrix("_ColorMatrix", ColorMatrix);
Where ColorMatrix
is a Property that generates Matrix4x4
.
Does anyone know how to make the colors persist inside the shader?
Move
SetColorMatrix();
fromOnInspectorGUI()
in the editor toOnValidate()
in the script.If that doesn't work, add
[ExecuteInEditMode]
before the class declaration and also addSetColorMatrix()
toStart()
.