Ok, here is my shader:
Shader "Custom/2DAnimatedTile"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
// Create the inspector values
_TotalFrames ("Total FRames", Float) = 0.0
_FPS ("FPS", Float) = 0.0
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
//Create the connection to the properties inside of the
//CG program
float _TotalFrames;
float _FPS;
float _Tick;
v2f vert (appdata_base v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : COLOR
{
//ANIMATION
//Lets calculate the width of a single cell in our
//sprite sheet and get a uv percentage that each cel takes up.
float frameUVPercentage = 1.0/_TotalFrames;
//Lets store our UVs in a variable
float2 spriteUV = i.texcoord;
_Tick = frac(( _Time.y + frameUVPercentage ) * _FPS) - frameUVPercentage;
spriteUV.x = ( spriteUV.x + frameUVPercentage ) + ( floor( _Tick * _TotalFrames ) * frameUVPercentage );
fixed4 col = tex2D(_MainTex, spriteUV);
return col;
}
ENDCG
}
}
}
Works perfectly with sprites, but... if I make a TileMap and use " Multiple Materials" on the tile sprite collection, the material is animated but is over sized and I have no idea where is the problem.
What am I doing wrong?
Some idea or clue?