forked from mob-sakai/SoftMaskForUGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftMaskableEditor.cs
More file actions
138 lines (115 loc) · 4.42 KB
/
Copy pathSoftMaskableEditor.cs
File metadata and controls
138 lines (115 loc) · 4.42 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using MaskIntr = UnityEngine.SpriteMaskInteraction;
namespace Coffee.UISoftMask
{
internal enum MaskInteraction : int
{
VisibleInsideMask = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6),
VisibleOutsideMask = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6),
Custom = -1,
}
/// <summary>
/// SoftMaskable editor.
/// </summary>
[CustomEditor(typeof(SoftMaskable))]
[CanEditMultipleObjects]
public class SoftMaskableEditor : Editor
{
private static readonly List<Mask> s_TmpMasks = new List<Mask>();
private static GUIContent s_MaskWarning;
private SerializedProperty _spMaskInteraction;
private bool _custom;
private MaskInteraction maskInteraction
{
get
{
var value = _spMaskInteraction.intValue;
return _custom
? MaskInteraction.Custom
: System.Enum.IsDefined(typeof(MaskInteraction), value)
? (MaskInteraction) value
: MaskInteraction.Custom;
}
set
{
_custom = (value == MaskInteraction.Custom);
if (!_custom)
{
_spMaskInteraction.intValue = (int) value;
}
}
}
private void OnEnable()
{
_spMaskInteraction = serializedObject.FindProperty("m_MaskInteraction");
_custom = (maskInteraction == MaskInteraction.Custom);
if (s_MaskWarning == null)
{
s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"), "This is not a SoftMask component.");
}
}
private void DrawMaskInteractions()
{
var softMaskable = target as SoftMaskable;
if (softMaskable == null) return;
softMaskable.GetComponentsInParent<Mask>(true, s_TmpMasks);
s_TmpMasks.RemoveAll(x => !x.enabled);
s_TmpMasks.Reverse();
maskInteraction = (MaskInteraction) EditorGUILayout.EnumPopup("Mask Interaction", maskInteraction);
if (!_custom) return;
var l = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 45;
using (var ccs = new EditorGUI.ChangeCheckScope())
{
var intr0 = DrawMaskInteraction(0);
var intr1 = DrawMaskInteraction(1);
var intr2 = DrawMaskInteraction(2);
var intr3 = DrawMaskInteraction(3);
if (ccs.changed)
{
_spMaskInteraction.intValue = (intr0 << 0) + (intr1 << 2) + (intr2 << 4) + (intr3 << 6);
}
}
EditorGUIUtility.labelWidth = l;
}
private int DrawMaskInteraction(int layer)
{
var mask = layer < s_TmpMasks.Count ? s_TmpMasks[layer] : null;
var intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3);
if (!mask)
{
return (int) intr;
}
GUILayout.BeginHorizontal();
EditorGUILayout.LabelField(mask is SoftMask ? GUIContent.none : s_MaskWarning, GUILayout.Width(16));
GUILayout.Space(-5);
EditorGUILayout.ObjectField("Mask " + layer, mask, typeof(Mask), false);
GUILayout.Space(-15);
intr = (MaskIntr) EditorGUILayout.EnumPopup(intr);
GUILayout.EndHorizontal();
return (int) intr;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
DrawMaskInteractions();
serializedObject.ApplyModifiedProperties();
var current = target as SoftMaskable;
if (current == null) return;
var mask = current.softMask;
if (mask) return;
GUILayout.BeginHorizontal();
EditorGUILayout.HelpBox("This is unnecessary SoftMaskable.\nCan't find any SoftMask components above.", MessageType.Warning);
if (GUILayout.Button("Remove", GUILayout.Height(40)))
{
DestroyImmediate(current);
EditorUtils.MarkPrefabDirty();
}
GUILayout.EndHorizontal();
}
}
}