To create optimized shaderA program that runs on the GPU. More info
See in Glossary code for a platform, branch in your shader code using one of the preprocessor macros Unity defines.
#if defined(SHADER_API_DX11)
// Shader code for when the target graphics API is DirectX 11
#endif
Unity defines the following macros when you make the corresponding platform the target for your build.
| Macro | Platform |
|---|---|
UNITY_PLATFORM_ANDROID |
Android devices |
UNITY_PLATFORM_EMBEDDED_LINUX |
Embedded Linux devices |
UNITY_PLATFORM_GAMECORE_XBOXONE |
Xbox One Game Core |
UNITY_PLATFORM_IOS |
iOS devices |
UNITY_PLATFORM_LINUX |
Linux devices |
UNITY_PLATFORM_META_QUEST |
Meta Quest devices. If you target Meta Quest, Unity also defines the UNITY_PLATFORM_ANDROID macro. |
UNITY_PLATFORM_OSX |
macOS devices |
UNITY_PLATFORM_PS4 |
PlayStation®4 |
UNITY_PLATFORM_PS5 |
PlayStation®5 |
UNITY_PLATFORM_QNX |
QNX devices |
UNITY_PLATFORM_SWITCH |
Nintendo Switch™ |
UNITY_PLATFORM_SWITCH2 |
Nintendo Switch™ 2 |
UNITY_PLATFORM_TVOS |
tvOS devices |
UNITY_PLATFORM_UWP |
Universal Windows Platform (UWP) devices |
UNITY_PLATFORM_VISIONOS |
visionOS devices |
UNITY_PLATFORM_WEBGL |
WebGLA JavaScript API that renders 2D and 3D graphics in a web browser. The Unity Web build option allows Unity to publish content as JavaScript programs which use HTML5 technologies and the WebGL rendering API to run Unity content in a web browser. More info See in Glossary devices |
UNITY_PLATFORM_WINDOWS |
Windows devices |
UNITY_PLATFORM_XBOXONE |
Xbox One |
To branch again within platform-specific branching code, declare a shader keyword after you test for the platform, then branch using the keyword. For example:
// Test if the build target is Meta Quest
#if defined(UNITY_PLATFORM_META_QUEST)
// Add a keyword only if the build target is Meta Quest
#pragma shader_feature _ META_QUEST_REFLECTIONS
#endif
...
fixed4 frag (v2f i) : SV_Target
{
// Test if the build target is Meta Quest
#if defined(UNITY_PLATFORM_META_QUEST)
// Test if your custom keyword is enabled
#if META_QUEST_REFLECTIONS
// Shader code for Meta Quest with reflections
#else
// Shader code for Meta Quest without reflections
#endif
#endif
...
}
For more information, refer to Changing how shaders work using keywords.
Unity defines the following macros when Unity compiles shaders for the corresponding graphics API.
| Macro | Graphics API |
|---|---|
SHADER_API_DX11 |
Direct3D 11 |
SHADER_API_DX12 |
Direct3D 12 |
SHADER_API_D3D11 |
Direct3D 11 and Direct3D 12 (marked for deprecation) |
SHADER_API_D3D11_9X |
DirectX 11 feature level 9, for Universal Windows Platform (UWP) devices |
SHADER_API_DESKTOP |
Graphics APIs used by Windows devices, macOS devices, Linux desktop platforms, and web devices |
SHADER_API_GLCORE |
OpenGL 3.x, OpenGL 4.x |
SHADER_API_GLES3 |
OpenGL ES 3.x |
SHADER_API_METAL |
Metal on iOS or macOS |
SHADER_API_MOBILE |
Graphics APIs used by iOS and Android mobile devices, and tvOS devices |
SHADER_API_VULKAN |
Vulkan |
SHADER_API_WEBGPU |
WebGPU |
SHADER_TARGET_GLSL |
OpenGL and GLES platforms where Unity compiles HLSL to GLSL |