I would like to have a shader selection drop-down (similar to that found in the material inspector) to allow users to select the shader that they would like to use with batch-generated materials.
selectedShader = ???.ShaderField(selectedShader);
If there is no way to reuse the material inspector field, how can this be reproduced?
// Create and cache a dummy material
if (_dummyMaterial == null) {
_dummyMaterial = new Material(Shader.Find("Diffuse"));
_dummyMaterial.hideFlags = HideFlags.HideAndDontSave;
}
// First ensure that all shader assets are loaded
UnityEditorInternal.InternalEditorUtility.SetupShaderMenu(_dummyMaterial);
// Fetch all shader assets
Shader[] shaders = (Shader[])UnityEngine.Resources.FindObjectsOfTypeAll(typeof(Shader));
// Filter out those that are supposed to be hidden
shaders = shaders.Where(s => s != null && s.name != "" && !s.name.StartsWith("__")).ToArray();
This appears to be a better solution than my previous one because it works without having previously selected the shader popup menu for a material. Not perfect, but as good as I could figure out.
Maybe you would like to have a look at String-O-Matic. You can have a nice list of available shaders as a dropdown in the inspector. You would still need to call Shader.Find, but at least you know the shader in question exists.