ShaderSettingsDrawer.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace Siccity.GLTFUtility {
  6. [CustomPropertyDrawer(typeof(ShaderSettings))]
  7. public class ShaderSettingsDrawer : PropertyDrawer {
  8. private static SerializedObject serializedGraphicsSettings;
  9. private bool hasShaders;
  10. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
  11. EditorGUI.BeginProperty(position, label, property);
  12. // Get shader properties
  13. SerializedProperty metalicProp = property.FindPropertyRelative("metallic");
  14. SerializedProperty metallicBlendProp = property.FindPropertyRelative("metallicBlend");
  15. SerializedProperty specularProp = property.FindPropertyRelative("specular");
  16. SerializedProperty specularBlendProp = property.FindPropertyRelative("specularBlend");
  17. hasShaders = HasShaders(metalicProp, metallicBlendProp, specularProp, specularBlendProp);
  18. EditorGUI.PropertyField(position, property, true);
  19. // Show shader warning
  20. if (!hasShaders && property.isExpanded) {
  21. GUIContent content = new GUIContent("Shaders not found in 'GraphicSettings/Always Included Shaders'.\nShaders might not be available for runtime import.");
  22. float baseHeight = EditorGUI.GetPropertyHeight(property, true);
  23. Rect warningRect = new Rect(position.x, position.y + baseHeight + 2, position.width, 50);
  24. EditorGUI.HelpBox(warningRect, content.text, MessageType.Warning);
  25. /* Rect buttonRect = new Rect(warningRect.xMax - 150, warningRect.yMax + 2, 150, 18);
  26. if (GUI.Button(buttonRect, "Open GraphicsSettings")) {
  27. } */
  28. }
  29. EditorGUI.EndProperty();
  30. }
  31. public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
  32. float height = EditorGUI.GetPropertyHeight(property, true);
  33. if (!hasShaders && property.isExpanded) height += 40;
  34. return height;
  35. }
  36. public bool HasShaders(params SerializedProperty[] shaders) {
  37. if (serializedGraphicsSettings == null) {
  38. GraphicsSettings graphicsSettings = AssetDatabase.LoadAssetAtPath<GraphicsSettings>("ProjectSettings/GraphicsSettings.asset");
  39. serializedGraphicsSettings = new SerializedObject(graphicsSettings);
  40. }
  41. SerializedProperty arrayProp = serializedGraphicsSettings.FindProperty("m_AlwaysIncludedShaders");
  42. bool[] hasShaders = new bool[shaders.Length];
  43. for (int i = 0; i < arrayProp.arraySize; ++i) {
  44. SerializedProperty arrayElem = arrayProp.GetArrayElementAtIndex(i);
  45. for (int k = 0; k < shaders.Length; k++) {
  46. if (shaders[k].objectReferenceValue == arrayElem.objectReferenceValue) hasShaders[k] = true;
  47. }
  48. }
  49. return hasShaders.All(x => x);
  50. }
  51. }
  52. }