- Home /
Can I get the a list of the properties in a shader at runtime?
Hey Im working on a runtime editor and Im wondering if theres any way to be able to get a list of the properties a shader uses at runtime. An array that could tell me the name of that property and what type is it. Is there any way to do this at runtime?
Answer by robertbu · Jul 27, 2013 at 05:24 PM
This is in an area of Unity I've never poked around in before, so I thought I see what I could dig up. I don't think there is a way to directly get what you are asking for. But I don't know of any way to dynamically add a shader to a built app, so the universe of possible shaders is known at edit time. So if you generate a dictionary of property/type pairs at edit time, you can use Material.HasProp() with each entry from the dictionary to generate a list of properties for the specific shader. As an alternate, you could build a database of the properties that each shader has as edit time, and just do a lookup at runtime.
So the first step in edit time processing is to get access to all the shaders in the project. I could not find a way to dynamically get the list of built-in shaders, but any shaders you've added can be accessed this way:
string[] paths = AssetDatabase.GetAllAssetPaths();
foreach( string s in paths ) {
Shader shader = (Shader)AssetDatabase.LoadAssetAtPath( s, typeof( Shader ) );
if( shader != null ) {
Debug.Log( shader.name);
}
}
As for the built-in shaders, you can get at them using Shader.Find() if you have their names. To get the names, first get the zip file that contains the built-in shaders for the version of Unity you are running from here:
[http://unity3d.com/unity/download/archive][2]
A quick script produces this list of shaders for Unity 4.2:
"Hidden/Camera-DepthNormalTexture",
"Hidden/Camera-DepthTexture",
"GUI/Text Shader",
"Hidden/BlitCopy",
"Hidden/InternalClear",
"Hidden/Internal-CombineDepthNormals",
"Hidden/Internal-Flare",
"Hidden/Internal-GUITexture",
"Hidden/Internal-GUITextureBlit",
"Hidden/Internal-GUITextureClip",
"Hidden/Internal-GUITextureClipText",
"Hidden/Internal-Halo",
"Hidden/Internal-PrePassCollectShadows",
"Hidden/Internal-PrePassLighting",
"Hidden/Shadow-ScreenBlur",
"Hidden/Shadow-ScreenBlurRotated",
"Transparent/Bumped Diffuse",
"Transparent/Bumped Specular",
"Transparent/Diffuse",
"Transparent/Specular",
"Transparent/Parallax Diffuse",
"Transparent/Parallax Specular",
"Transparent/VertexLit",
"Transparent/Cutout/Bumped Diffuse",
"Transparent/Cutout/Bumped Specular",
"Transparent/Cutout/Diffuse",
"Transparent/Cutout/Specular",
"Transparent/Cutout/Soft Edge Unlit",
"Transparent/Cutout/VertexLit",
"Decal",
"FX/Flare",
"Self-Illumin/Bumped Diffuse",
"Self-Illumin/Bumped Specular",
"Self-Illumin/Diffuse",
"Self-Illumin/Specular",
"Self-Illumin/Parallax Diffuse",
"Self-Illumin/Parallax Specular",
"Self-Illumin/VertexLit",
"Legacy Shaders/Lightmapped/Bumped Diffuse",
"Legacy Shaders/Lightmapped/Bumped Specular",
"Legacy Shaders/Lightmapped/Diffuse",
"Legacy Shaders/Lightmapped/Specular",
"Legacy Shaders/Lightmapped/VertexLit",
"Bumped Diffuse",
"Bumped Specular",
"Diffuse",
"Diffuse Detail",
"Legacy Shaders/Diffuse Fast",
"Specular",
"Parallax Diffuse",
"Parallax Specular",
"VertexLit",
"Particles/Additive",
"Particles/~Additive-Multiply",
"Particles/Additive (Soft)",
"Particles/Alpha Blended",
"Particles/Blend",
"Particles/Multiply",
"Particles/Multiply (Double)",
"Particles/Alpha Blended Premultiply",
"Particles/VertexLit Blended",
"Reflective/Bumped Diffuse",
"Reflective/Bumped Unlit",
"Reflective/Bumped Specular",
"Reflective/Bumped VertexLit",
"Reflective/Diffuse",
"Reflective/Specular",
"Reflective/Parallax Diffuse",
"Reflective/Parallax Specular",
"Reflective/VertexLit",
"RenderFX/Skybox Cubed",
"RenderFX/Skybox",
"Mobile/Bumped Diffuse",
"Mobile/Bumped Specular (1 Directional Light)",
"Mobile/Bumped Specular",
"Mobile/Diffuse",
"Mobile/Unlit (Supports Lightmap)",
"Mobile/Particles/Additive",
"Mobile/Particles/VertexLit Blended",
"Mobile/Particles/Alpha Blended",
"Mobile/Particles/Multiply",
"Mobile/Skybox",
"Mobile/VertexLit (Only Directional Lights)",
"Mobile/VertexLit",
"Nature/Tree Soft Occlusion Bark",
"Hidden/Nature/Tree Soft Occlusion Bark Rendertex",
"Nature/Tree Soft Occlusion Leaves",
"Hidden/Nature/Tree Soft Occlusion Leaves Rendertex",
"Hidden/Nature/Terrain/Bumped Specular AddPass",
"Nature/Terrain/Bumped Specular",
"Nature/Tree Creator Bark",
"Hidden/Nature/Tree Creator Bark Optimized",
"Hidden/Nature/Tree Creator Bark Rendertex",
"Nature/Tree Creator Leaves",
"Nature/Tree Creator Leaves Fast",
"Hidden/Nature/Tree Creator Leaves Fast Optimized",
"Hidden/Nature/Tree Creator Leaves Optimized",
"Hidden/Nature/Tree Creator Leaves Rendertex",
"Sprites/Alpha Blended",
"Sprites/Pixel Snap/Alpha Blended",
"Hidden/TerrainEngine/Details/Vertexlit",
"Hidden/TerrainEngine/Details/WavingDoublePass",
"Hidden/TerrainEngine/Details/BillboardWavingDoublePass",
"Hidden/TerrainEngine/Splatmap/Lightmap-AddPass",
"Nature/Terrain/Diffuse",
"Hidden/TerrainEngine/BillboardTree",
"Unlit/Transparent",
"Unlit/Transparent Cutout",
"Unlit/Texture"
As for the properties themselves, you an use the ShaderUtil class:
string st = ShaderUtil.GetPropertyName (shader, j);
int type = (int)ShaderUtil.GetPropertyType(shader, j);
And there is a list of the properties and their types for the above listed built-in shaders:
_MainTex,4
_Color,0
_BumpMap,4
_SpecColor,0
_Shininess,3
_Parallax,3
_ParallaxMap,4
_Emission,0
_Cutoff,3
_DecalTex,4
_Illum,4
_EmissionLM,2
_LightMap,4
_Detail,4
_TintColor,0
_InvFade,3
_EmisColor,0
_ReflectColor,0
_Cube,4
_Tint,0
_Tex,4
_FrontTex,4
_BackTex,4
_LeftTex,4
_RightTex,4
_UpTex,4
_DownTex,4
_BaseLight,3
_AO,3
_Scale,1
_SquashAmount,2
_Occlusion,3
_Control,4
_Splat3,4
_Splat2,4
_Splat1,4
_Splat0,4
_Normal3,4
_Normal2,4
_Normal1,4
_Normal0,4
_GlossMap,4
_TranslucencyMap,4
_ShadowOffset,4
_TranslucencyColor,0
_TranslucencyViewDependency,3
_ShadowStrength,3
[2]: http://unity3d.com/unity/download/archive
Your answer
Follow this Question
Related Questions
UV mapping manipulation 0 Answers
"Setcolor " is permanently changing the color of shader in Assets 0 Answers
Building a Unity 5 Standard Material at runtime 3 Answers
Shader.Find() equivalent for Compute Shaders? 2 Answers
Multi materials for one mesh 0 Answers