- Home /
Change dynamicaly a PNG used in a motion
Hi everyone,
I have an enemy with a specific animator and few motions with 2D sprites. I want to have the same enemy with different colors (so duplicated PNGs with other colors), but the only way I see is to duplicate all the motions and set them again and again. So is there a way to switch at runtime the PNGs used (or better the folder), as we can do in HTML5 ? (I can't use a simple tint on the material, cause I don't want to modify the skin color, only the clothes)
Thanks in advance.
PS : sorry for my bad english.
Ins$$anonymous$$d of having a ton of textures for different colors and waste vram like that, you can program a shader which will take a b&w mask to multiply with the tint color. So only the parts you want to color will be colored. Let me know if you need help with this and I can code the shader for you.
Thanks for your help. Sorry I never used mask in Unity, is that a sprite I have to add ? I have many sprites for only 1 enemy, so do I have to make a mask for each sprite ?
Answer by SarperS · Oct 22, 2014 at 02:15 PM
It seems I can't attach a unitypackage file here so I've put it up on my dropbox. Please download the file and import it in your project. It contains a shader, 2 sprites and a demo scene. Let me know if you can't figure out how to make it work.
https://dl.dropboxusercontent.com/u/1652836/SpriteMaskedColorTint.unitypackage
It works well thank you for the tip, even if with that I still need to make a mask for each sprite, but 2 of each better than 5 (or more) :) I just need to adapt it with the Sprite/Diffuse shader ins$$anonymous$$d of the Sprite/Default one, in order to recieve light.
You are welcome, please let me know if you have problems converting the shader to the diffuse version and I will do it for you. Please mark the answer as the solution.
Yes sorry I'm not used to modify shaders, i got some syntax errors with these pragma, if it doesn't bother you it would be wonderful to help me with it.
Sure, here you go, diffuse version. What do you mean by "modify the mask in realtime"? Like change the mask texture in runtime?
Shader "Sprites/Diffuse $$anonymous$$asked Tint"
{
Properties
{
[PerRendererData] _$$anonymous$$ainTex ("Sprite Texture", 2D) = "white" {}
_$$anonymous$$ask("Color $$anonymous$$ask", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[$$anonymous$$aterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { $$anonymous$$ode Off }
Blend One One$$anonymous$$inusSrcAlpha
CGPROGRA$$anonymous$$
#pragma surface surf Lambert vertex:vert
#pragma multi_compile DU$$anonymous$$$$anonymous$$Y PIXELSNAP_ON
sampler2D _$$anonymous$$ainTex;
sampler2D _$$anonymous$$ask;
fixed4 _Color;
struct Input {
float2 uv_$$anonymous$$ainTex;
fixed4 color;
};
void vert (inout appdata_full v, out Input o) {
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
v.normal = float3(0,0,-1);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 mask = tex2D(_$$anonymous$$ask, IN.uv_$$anonymous$$ainTex);
fixed4 color = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
fixed4 finalC = lerp(color, color * IN.color, mask);
o.Albedo = finalC.rgb * finalC.a;
o.Alpha = finalC.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Thanks a lot ! The only problem remaining is that in the motion, when I click on "Add Curve", I don't have the option to change the mask sprite, only "$$anonymous$$aask_ST" and "$$anonymous$$ask_Texel Size" :/