I would like to be able to make a Unity Sprite flash 2 colors:
- The entire sprite should flash Red (or whatever color is defined in the Sprite Color setting).
- There should be a Black outline around the Sprite.
DMGregory wrote the following code which modified the default Sprite Shader to wonderfully achieve the first goal:
Pass
{
CGPROGRAM
#pragma vertex SpriteVert
#pragma fragment ColorLerpFrag // Use our version instead of the default.
#pragma target 2.0
#pragma multi_compile_instancing
#pragma multi_compile_local _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnitySprites.cginc"
fixed4 ColorLerpFrag(v2f IN) : SV_Target
{
// Sample sprite texture.
fixed4 c = SampleSpriteTexture (IN.texcoord);
// Blend toward sprite component color depending on its alpha.
c.rgb = lerp(IN.color.rgb, c.rgb, IN.color.a);
// Pre-multiply alpha to work with the blending function.
c.rgb *= c.a;
return c;
}
ENDCG
}
I have tried modifying the shader to also add a black outline around the Sprite, but can't get it to work.
Another possibility is to clone the GameObject with the Sprite Component, center the Sprite, turn it Black and then scale it up slightly. But it would be much better if this effect could be achieved with a shader, as cloning would add to the memory footprint.
Is it possible to modify the shader further to also create a black outline around the Sprite?