Advertisement
Guest User

ShaderField for Unity editor GUI's

a guest
Jan 3rd, 2013
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. // Warning, this depends upon undocumented internal method:
  2. // UnityEditorInternal.InternalEditorUtility.SetupShaderMenu
  3.  
  4. public static class ShaderMenuUtility {
  5.  
  6.     #region Helper
  7.  
  8.     private static GUIContent _tempText = new GUIContent();
  9.  
  10.     private static GUIContent TempContent(string text) {
  11.         _tempText.text = text;
  12.         return _tempText;
  13.     }
  14.  
  15.     #endregion
  16.  
  17.     #region Shader Name Cache
  18.  
  19.     private static GUIContent[] _shaderNames;
  20.  
  21.     private static void ClearShaderCache() {
  22.         _shaderNames = null;
  23.     }
  24.  
  25.     private static Material _dummyMaterial;
  26.  
  27.     private static void PrepareShaderCache() {
  28.         if (_dummyMaterial == null) {
  29.             _dummyMaterial = new Material(Shader.Find("Diffuse"));
  30.             _dummyMaterial.hideFlags = HideFlags.HideAndDontSave;
  31.         }
  32.  
  33.         // This is a little wasteful, but unfortunately needed :/
  34.         UnityEditorInternal.InternalEditorUtility.SetupShaderMenu(_dummyMaterial);
  35.  
  36.         // Fetch shaders and filter
  37.         Shader[] shaders = (Shader[])UnityEngine.Resources.FindObjectsOfTypeAll(typeof(Shader));
  38.         shaders = shaders.Where(s => s != null && s.name != "" && !s.name.StartsWith("__")).ToArray();
  39.  
  40.         // Generate list of shader names
  41.         _shaderNames = shaders.Select(s => new GUIContent(s.name)).ToArray();
  42.     }
  43.  
  44.     #endregion
  45.  
  46.     // EditorGUI style version
  47.     public static Shader ShaderField(Rect position, string label, Shader shader) {
  48.         int controlID = GUIUtility.GetControlID(FocusType.Passive);
  49.         EventType eventType = Event.current.GetTypeForControl(controlID);
  50.        
  51.         // Clear shader cache before layout is processed for control
  52.         if (eventType == EventType.Layout)
  53.             ClearShaderCache();
  54.        
  55.         if (!string.IsNullOrEmpty(label))
  56.             position = EditorGUI.PrefixLabel(position, controlID, TempContent(label));
  57.  
  58.         // Prepare list of shaders
  59.         if (_shaderNames == null)
  60.             PrepareShaderCache();
  61.        
  62.         int selectedIndex = (shader != null)
  63.             ? System.Array.FindIndex(_shaderNames, c => c.text == shader.name)
  64.             : -1;
  65.        
  66.         EditorGUI.BeginChangeCheck();
  67.         selectedIndex = EditorGUI.Popup(position, selectedIndex, _shaderNames);
  68.         if (EditorGUI.EndChangeCheck()) {
  69.             shader = (selectedIndex >= 0 && selectedIndex < _shaderNames.Length)
  70.                 ? Shader.Find(_shaderNames[selectedIndex].text)
  71.                 : null;
  72.         }
  73.  
  74.         return shader;
  75.     }
  76.  
  77.     // EditorGUILayout style version
  78.     public static Shader ShaderField(string label, Shader shader, params GUILayoutOption[] options) {
  79.         Rect position = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.popup, options);
  80.         return ShaderField(position, label, shader);
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement