> For the complete documentation index, see [llms.txt](https://vilyx.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vilyx.gitbook.io/project/glava-tretya.md).

# Глава третья

Изменений свойств материала из C#

&#x20;Шейдер, описанный в этой главе, будет состоять из двух частей, самого шейдера и крохотного кусочка кода на C#.

```csharp
Shader "Tutorial/AnimationSequence"
{
	Properties
	{
		_MainTex("Animation Texture", 2D) = "white" {}
		_WidthFramesCount("Width Frames count", Range(0, 20)) = 9
		_HeightFramesCount("Height Frames count", Range(0, 20)) = 4
		_CurrentFrame("Frames count", Range(0, 70)) = 0
	}
	SubShader
	{
		Cull Off
		ZWrite Off
		ZTest Always
        //Следующая строка позволяет визуализировать текстуры,
        // содержащие альфаканал, как полупрозрачные.
		Blend SrcAlpha OneMinusSrcAlpha

		Pass
		{
			CGPROGRAM
			#pragma vertex vert_img 
			#pragma fragment frag 

			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float _WidthFramesCount;
			float _HeightFramesCount;
			uint _CurrentFrame;

			fixed4 frag(v2f_img i) : COLOR
			{ 
				float2 uv = i.uv;
				uint frame = _CurrentFrame % (_WidthFramesCount*_HeightFramesCount);
				uv.x = (uv.x + frame % _WidthFramesCount) / _WidthFramesCount;
				uv.y = (uv.y + floor(frame / _WidthFramesCount)) / _HeightFramesCount;

				half4 base = tex2D(_MainTex, uv);
				return base;
			}
			ENDCG
		}
	}
}
```

&#x20;Этому шейдеру для работы нужна картинка состоящая из кадров анимации. \_WidthFramesCount - число кадров в ширину на картинке, \_HeightFramesCount - число кадров в высоту. ![](https://vilyx.gitbooks.io/shaders-lesson1/content/professor_walk_cycle_no_hat.png)

И C# код:

```csharp
using UnityEngine;
using System.Collections;

public class ShaderVariableSetter : MonoBehaviour {

	public Material sequenceMaterial;
	
	void Update () {
		sequenceMaterial.SetInt("_CurrentFrame", Mathf.FloorToInt(Time.time*12));
	}
}
```

Этот класс надо прикрепить к одному из объектов в сцене, а потом в поле sequenceMaterial перетянуть материал из вкладки Project

&#x20;![](https://vilyx.gitbooks.io/shaders-lesson1/content/shader.png)

После этих манипуляций строка:

```
sequenceMaterial.SetInt("_CurrentFrame", Mathf.FloorToInt(Time.time*12));
```

&#x20;будет менять переменную \_CurrentFrame внутри шейдера. Важно, что если переменная внутри шейдера типа float, то и устанавливать её надо функцией SetFloat, а в нашем случае uint и мы используем функцию SetInt.
