Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by numberkruncher · Aug 24, 2012 at 04:24 PM · guishadereditorfield

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Acegikmo · Sep 25, 2013 at 09:46 PM

I finally managed to figure out a definitive solution, which works for me, running Unity 4.2:

         MenuCommand mc;
         private void DisplayShaderContext(Rect r) {
             if( mc == null )
                 mc = new MenuCommand( this, 0 );

             // Create dummy material to make it not highlight any shaders inside:
             string tmpStr = "Shader \"Hidden/tmp_shdr\"{SubShader{Pass{}}}";
             Material temp = new Material( tmpStr ); 
             
             // Rebuild shader menu:
             UnityEditorInternal.InternalEditorUtility.SetupShaderMenu( temp );

             // Destroy temporary shader and material:
             DestroyImmediate( temp.shader, true );
             DestroyImmediate( temp, true ); 

             // Display shader popup:
             EditorUtility.DisplayPopupMenu( r, "CONTEXT/ShaderPopup", mc ); 
         }
         private void OnSelectedShaderPopup( string command, Shader shader ) {
             if( shader != null ) {
                 Debug.Log("Clicked shader: " + shader.name);
             }
         }


The other methods didn't work for me, they didn't show all shaders, but this one did :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Sep 25, 2013 at 09:52 PM 0
Share

Wow! nice find, cheers!

avatar image khan-amil · Jan 21, 2014 at 10:52 AM 0
Share

Thanks, works great. I struggled a bit to understand how to use it, but it simply works by calling something like :

 if (GUILayout.Button("Change shader"))
 {
  DisplayShaderContext(GUILayoutUtility.GetRect(GUIContent.none,EditorStyles.popup));
 }

I'm not sure I understand how unity end up calling OnSelectedShaderPopup, but it gets called on picking from the list all right.

avatar image Bunny83 · Jan 21, 2014 at 11:15 AM 0
Share

Well, Unity probably uses Send$$anonymous$$essage / Reflection like most the time. It's quite inefficient to create and destroy a material (and shader) each call. It's better to create it in OnEnable and destroy it in OnDisable.

avatar image Steven-Walker · Mar 11, 2014 at 10:25 PM 0
Share

@khan-amil: OnSelectedShaderPopup is a callback method to the object passed to $$anonymous$$enuCommand: mc = new $$anonymous$$enuCommand(myEditorClass, 0);

avatar image
1

Answer by ScroodgeM · Aug 26, 2012 at 07:08 PM

C#

ShaderToImport = EditorGUILayout.ObjectField(ShaderToImport, typeof(Shader), true) as Shader;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Aug 26, 2012 at 11:07 PM 0
Share

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

avatar image ScroodgeM · Aug 27, 2012 at 07:28 AM 0
Share

http://answers.unity3d.com/questions/12858/list-built-in-shaders-in-inspector.html

avatar image
1

Answer by numberkruncher · Sep 05, 2012 at 02:48 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ian Smithers · Jan 03, 2013 at 02:29 AM 0
Share

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

avatar image numberkruncher · Jan 03, 2013 at 02:22 PM 0
Share

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

avatar image azeitler · Sep 16, 2013 at 06:48 AM 0
Share

this does not work anymore in Unity 4.2.1. It only lists a couple of the standard shaders but not all of them. Any ideas why and how to solve it?

avatar image
0

Answer by Cobo3 · Feb 10, 2016 at 05:39 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Open GUI Editor? 1 Answer

Why are all my GUITexture elements semi-transparent? 0 Answers

Trigger an event from editor script? 0 Answers

Custom Material Editor 1 Answer

Why changing material shader at runtime also affect my asset on disk? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges