The Properties block
- Game Development course
- Advanced
- Lesson 4/71
- 4 min
Learn to take outside values with the Properties block.
Declaring Variables with the Properties Block
The Properties { } block at the very top of ShaderLab exposes shader parameters to the user in the Unity Inspector. Fields like color, numbers, sliders (Range), and textures are defined here.
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}Every value adjusted in the Inspector is saved to the material asset and fed directly into the shader calculations.
C# and Shader Variable Binding
A variable declared in the Properties block (like _Color) can be updated directly from C#. For this, Material.SetColor or Material.SetFloat methods are used.
// Updating a shader property from C#
Renderer rend = GetComponent<Renderer>();
rend.material.SetColor("_Color", Color.red);Inside the CGPROGRAM block of the shader, a matching uniform variable (fixed4 _Color;) is declared with the identical name.
Practice Update material property
Define a Start method. Inside, declare string ozellikAdi = "_Color"; and print "Guncellenen ozellik: " + ozellikAdi to the console.
Starter code
using UnityEngine;
public class PropertyLogger : MonoBehaviour
{
// Start icinde ozellik degiskenini tanimla ve yazdir
}Sample solution
using UnityEngine;
public class PropertyLogger : MonoBehaviour
{
void Start()
{
string ozellikAdi = "_Color";
Debug.Log("Guncellenen ozellik: " + ozellikAdi);
}
}Expected output
Guncellenen ozellik: _Color
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which type is used for a property defined in ShaderLab to appear as a slider in the Inspector?
- Boolean
- Color
- Texture2D
- Range(min, max) Correct answer
Explanation: In the ShaderLab Properties block, the 'Range(min, max)' type creates a numeric slider UI in the Inspector.
2 Properties color declaration
Properties { _Color ("Main Color", ____) = (1,1,1,1) }
Properties { _Color ("Main Color", Color) = (1,1,1,1) }
Explanation: The Color type is used to declare a color field in the ShaderLab Properties block.
3 Properties terms
| Term | Meaning |
|---|---|
Properties { } |
Block of exposed parameters |
Range(0, 1) |
Numeric value presenting a slider UI |
2D |
Texture input type |
SetColor |
Method updating color parameter from C# |