Challenge 1: Shader property
- Game Development course
- Advanced
- Lesson 67/71
- 5 min
Complete a shader skeleton with a Properties block.
Challenge 1: Shader Properties
In ShaderLab, the Properties block exposes parameters to the Inspector. Colors, numbers, range sliders, and textures are declared here.
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_MainTex ("Albedo", 2D) = "white" {}
}These exposed attributes are directly accessible from C# scripts via material.SetColor and material.SetTexture.
Practice Shader property name test
Define a Start method. Inside, declare string propertyName = "_MainColor"; and print "Shader ozelligi: " + propertyName to the console.
Starter code
using UnityEngine;
public class ShaderPropertyTester : MonoBehaviour
{
// Start icinde propertyName tanimla ve yazdir
}Sample solution
using UnityEngine;
public class ShaderPropertyTester : MonoBehaviour
{
void Start()
{
string propertyName = "_MainColor";
Debug.Log("Shader ozelligi: " + propertyName);
}
}Expected output
Shader ozelligi: _MainColor
Practice Shader glossiness value test
Define a Start method. Inside, declare float parlaklik = 0.8f; and print "Parlaklik degeri: " + parlaklik to the console.
Starter code
using UnityEngine;
public class ShaderGlossTester : MonoBehaviour
{
// Start icinde parlaklik tanimla ve yazdir
}Sample solution
using UnityEngine;
public class ShaderGlossTester : MonoBehaviour
{
void Start()
{
float parlaklik = 0.8f;
Debug.Log("Parlaklik degeri: " + parlaklik);
}
}Expected output
Parlaklik degeri: 0.8
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When '_MainTex ("Texture", 2D) = "white" {}' is declared in ShaderLab Properties, which method is called from C# to assign this texture?
- material.SetPhysics
- material.SetAnimation
- material.SetTexture Correct answer
- material.SetAudio
Explanation: To update 2D texture shader parameters via code, the material.SetTexture function is invoked.
2 Declare range slider property
Properties { _Glossiness ("Smoothness", ____(0, 1)) = 0.5 }
Properties { _Glossiness ("Smoothness", Range(0, 1)) = 0.5 }
Explanation: The 'Range' keyword is used in ShaderLab to expose a numerical slider field in the Inspector.
3 Shader property match
| Term | Meaning |
|---|---|
Properties |
Shader parameters edited in Inspector |
_Color |
Color parameter name for shader |
Range(0, 1) |
Float value with slider interface |
SetColor |
Updates color parameter from C# |