UIEffectEditor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEditor;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6. namespace Coffee.UIEffects.Editors
  7. {
  8. /// <summary>
  9. /// UIEffect editor.
  10. /// </summary>
  11. [CustomEditor(typeof(UIEffect))]
  12. [CanEditMultipleObjects]
  13. public class UIEffectEditor : Editor
  14. {
  15. SerializedProperty _spEffectMode;
  16. SerializedProperty _spEffectFactor;
  17. SerializedProperty _spColorMode;
  18. SerializedProperty _spColorFactor;
  19. SerializedProperty _spBlurMode;
  20. SerializedProperty _spBlurFactor;
  21. SerializedProperty _spAdvancedBlur;
  22. protected void OnEnable()
  23. {
  24. _spEffectMode = serializedObject.FindProperty("m_EffectMode");
  25. _spEffectFactor = serializedObject.FindProperty("m_EffectFactor");
  26. _spColorMode = serializedObject.FindProperty("m_ColorMode");
  27. _spColorFactor = serializedObject.FindProperty("m_ColorFactor");
  28. _spBlurMode = serializedObject.FindProperty("m_BlurMode");
  29. _spBlurFactor = serializedObject.FindProperty("m_BlurFactor");
  30. _spAdvancedBlur = serializedObject.FindProperty("m_AdvancedBlur");
  31. }
  32. public override void OnInspectorGUI()
  33. {
  34. //================
  35. // Effect setting.
  36. //================
  37. using (new MaterialDirtyScope(targets))
  38. EditorGUILayout.PropertyField(_spEffectMode);
  39. // When effect is enable, show parameters.
  40. if (_spEffectMode.intValue != (int) EffectMode.None)
  41. {
  42. EditorGUI.indentLevel++;
  43. EditorGUILayout.PropertyField(_spEffectFactor);
  44. EditorGUI.indentLevel--;
  45. }
  46. //================
  47. // Color setting.
  48. //================
  49. using (new MaterialDirtyScope(targets))
  50. EditorGUILayout.PropertyField(_spColorMode);
  51. // When color is enable, show parameters.
  52. {
  53. EditorGUI.indentLevel++;
  54. EditorGUILayout.PropertyField(_spColorFactor);
  55. EditorGUI.indentLevel--;
  56. }
  57. //================
  58. // Blur setting.
  59. //================
  60. using (new MaterialDirtyScope(targets))
  61. EditorGUILayout.PropertyField(_spBlurMode);
  62. // When blur is enable, show parameters.
  63. if (_spBlurMode.intValue != (int) BlurMode.None)
  64. {
  65. EditorGUI.indentLevel++;
  66. EditorGUILayout.PropertyField(_spBlurFactor);
  67. // When you change a property, it marks the material as dirty.
  68. using (new MaterialDirtyScope(targets))
  69. EditorGUILayout.PropertyField(_spAdvancedBlur);
  70. EditorGUI.indentLevel--;
  71. // Advanced blur requires uv2 channel.
  72. if (_spAdvancedBlur.boolValue)
  73. {
  74. ShowCanvasChannelsWarning();
  75. }
  76. }
  77. serializedObject.ApplyModifiedProperties();
  78. }
  79. void ShowCanvasChannelsWarning()
  80. {
  81. var effect = target as UIEffect;
  82. if (effect == null || !effect.graphic) return;
  83. var channel = effect.uvMaskChannel;
  84. var canvas = effect.graphic.canvas;
  85. if (canvas == null || (canvas.additionalShaderChannels & channel) == channel) return;
  86. EditorGUILayout.BeginHorizontal();
  87. {
  88. var msg = string.Format("Enable '{0}' of Canvas.additionalShaderChannels to use 'UIEffect'.", channel);
  89. EditorGUILayout.HelpBox(msg, MessageType.Warning);
  90. if (GUILayout.Button("Fix"))
  91. {
  92. canvas.additionalShaderChannels |= channel;
  93. }
  94. }
  95. EditorGUILayout.EndHorizontal();
  96. }
  97. }
  98. }