x


Select Shader in Custom Editor GUI

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?

more ▼

asked Aug 24 '12 at 04:24 PM

numberkruncher gravatar image

numberkruncher
1.9k 36 46 57

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I think that I have finally found my answer:

// 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.

more ▼

answered Sep 05 '12 at 02:48 AM

numberkruncher gravatar image

numberkruncher
1.9k 36 46 57

Hi Lea, how did you feed the Shader[] to your custom editor UI to show the selection drop down?

Jan 03 at 02:29 AM Ian Smithers

@Ian I have put an example together for you: http://pastebin.com/bbDhu0NX

Jan 03 at 02:22 PM numberkruncher
(comments are locked)
10|3000 characters needed characters left

C#

ShaderToImport = EditorGUILayout.ObjectField(ShaderToImport, typeof(Shader), true) as Shader;
more ▼

answered Aug 26 '12 at 07:08 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

Nice idea, though unfortunately this doesn't allow one to select from the built-in shaders. Do you know if there is a way to get a list of all available shaders? Perhaps such a list could be used to produce a similar but custom control. Thanks

Aug 26 '12 at 11:07 PM numberkruncher
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3674
x1665
x1650
x38

asked: Aug 24 '12 at 04:24 PM

Seen: 952 times

Last Updated: Jan 03 at 02:24 PM