Shader for animated tiles

I’m a noob in shaders programming, making a game with 2DToolkit and I want to get a shader to animate tiles in a tilemap ( I know I could control the animation with c# script but I want to do with a shader)

Is this possible?
Here is my shader:

Shader "Custom/AnimSprite"
{
    Properties
    {
        _MainTex ("Main Texture (RGB)", 2D) = "white" {}
      
        // Create the properties below
        _TotalFrames ("Total FRames", float) = 0.0
        _InitFrame ("Init Frame", float) = 0.0
        _FPS ("FPS", float) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
      
        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
      
        //Create the connection to the properties inside of the
        //CG program
        float _TotalFrames;
        float _InitFrame;
        float _FPS;
    
        int _Tick;

        struct Input
        {float2 uv_MainTex;};

        void surf (Input IN, inout SurfaceOutput o)
        {
            //Lets store our UVs in a seperate variable
            float2 spriteUV = IN.uv_MainTex;
          
            //Lets calculate the width of a single frame in our
            //sprite sheet and get a uv percentage that each frame takes up.
            float frameUVPercentage = 1.0/_TotalFrames;
          
            //Animate the uv's tiling
            float xValue = spriteUV.x;
        
            _Tick = _Time.y * _FPS;
        
            xValue += frameUVPercentage * _Tick;

            spriteUV = float2(xValue, spriteUV.y);
          
            half4 c = tex2D (_MainTex, spriteUV);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Using _Time.y the _Tick is increasing and I don’t know how to reset the _Tick value.
I have read about the use of sin with _Time but my idea is increasing the texture tiling and upon reaching at the final tiling, reset the tiling to the first tile.

Something like that:

Thank you.

You’ll have to do it with a script. Shaders don’t have global variables that they can set, so there is no option to reset the _Tick value. All global variables should be supplied externally. In other words, a shader has no memory. I doesn’t know what it did last frame.

If you let the time control the uv directly, you could do something like this:

spriteUV.x = (spriteUV.x + (frac(_Time.y * speed) * 10.0)) / 10.0;

Thank you very much** jvo3dc**, now I understand better the shader’s operations.

Here is a new version, closer from my target, but…

Shader "Custom/AnimSprite"
{
    Properties
    {
        _MainTex ("Main Texture (RGB)", 2D) = "white" {}
        
        // Create the properties below
        _TotalFrames ("Total FRames", float) = 0.0
        _FPS ("FPS", float) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        
        //Create the connection to the properties inside of the 
        //CG program
        float _TotalFrames;
        float _FPS;
      
        float _Tick;

        struct Input
        {float2 uv_MainTex;};

        void surf (Input IN, inout SurfaceOutput o)
        {   
            //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 = IN.uv_MainTex;
            _Tick = frac(( _Time.y + spriteUV.x ) * _FPS) - frameUVPercentage;
            spriteUV.x = (( spriteUV.x + frameUVPercentage ) + ( 0 * frameUVPercentage ) ) + ( floor( _Tick * _TotalFrames ) * frameUVPercentage );
            
            half4 c = tex2D (_MainTex, spriteUV);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

Here the package:

1822523–116622–ShaderAnimSprite.unitypackage (22.1 KB)