summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Editor/UIEffectEditor.cs
blob: 028ee7315f17951011771a0c922531f24887855f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using System.Linq;
using System;

namespace Coffee.UIEffects.Editors
{
    /// <summary>
    /// UIEffect editor.
    /// </summary>
    [CustomEditor(typeof(UIEffect))]
    [CanEditMultipleObjects]
    public class UIEffectEditor : Editor
    {
        SerializedProperty _spEffectMode;
        SerializedProperty _spEffectFactor;
        SerializedProperty _spColorMode;
        SerializedProperty _spColorFactor;
        SerializedProperty _spBlurMode;
        SerializedProperty _spBlurFactor;
        SerializedProperty _spAdvancedBlur;

        protected void OnEnable()
        {
            _spEffectMode = serializedObject.FindProperty("m_EffectMode");
            _spEffectFactor = serializedObject.FindProperty("m_EffectFactor");
            _spColorMode = serializedObject.FindProperty("m_ColorMode");
            _spColorFactor = serializedObject.FindProperty("m_ColorFactor");
            _spBlurMode = serializedObject.FindProperty("m_BlurMode");
            _spBlurFactor = serializedObject.FindProperty("m_BlurFactor");
            _spAdvancedBlur = serializedObject.FindProperty("m_AdvancedBlur");
        }

        public override void OnInspectorGUI()
        {
            //================
            // Effect setting.
            //================
            using (new MaterialDirtyScope(targets))
                EditorGUILayout.PropertyField(_spEffectMode);

            // When effect is enable, show parameters.
            if (_spEffectMode.intValue != (int) EffectMode.None)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(_spEffectFactor);
                EditorGUI.indentLevel--;
            }


            //================
            // Color setting.
            //================
            using (new MaterialDirtyScope(targets))
                EditorGUILayout.PropertyField(_spColorMode);

            // When color is enable, show parameters.
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(_spColorFactor);
                EditorGUI.indentLevel--;
            }


            //================
            // Blur setting.
            //================
            using (new MaterialDirtyScope(targets))
                EditorGUILayout.PropertyField(_spBlurMode);

            // When blur is enable, show parameters.
            if (_spBlurMode.intValue != (int) BlurMode.None)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(_spBlurFactor);

                // When you change a property, it marks the material as dirty.
                using (new MaterialDirtyScope(targets))
                    EditorGUILayout.PropertyField(_spAdvancedBlur);
                EditorGUI.indentLevel--;

                // Advanced blur requires uv2 channel.
                if (_spAdvancedBlur.boolValue)
                {
                    ShowCanvasChannelsWarning();
                }
            }

            serializedObject.ApplyModifiedProperties();
        }

        void ShowCanvasChannelsWarning()
        {
            var effect = target as UIEffect;
            if (effect == null || !effect.graphic) return;

            var channel = effect.uvMaskChannel;
            var canvas = effect.graphic.canvas;
            if (canvas == null || (canvas.additionalShaderChannels & channel) == channel) return;

            EditorGUILayout.BeginHorizontal();
            {
                var msg = string.Format("Enable '{0}' of Canvas.additionalShaderChannels to use 'UIEffect'.", channel);
                EditorGUILayout.HelpBox(msg, MessageType.Warning);
                if (GUILayout.Button("Fix"))
                {
                    canvas.additionalShaderChannels |= channel;
                }
            }
            EditorGUILayout.EndHorizontal();
        }
    }
}