2019-05-13 22:35:42 +08:00
|
|
|
using System.Collections.Generic;
|
2018-12-17 09:08:40 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using UnityEditor;
|
2019-01-26 20:30:41 +08:00
|
|
|
using MaskIntr = UnityEngine.SpriteMaskInteraction;
|
2018-12-17 09:08:40 +08:00
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
namespace Coffee.UISoftMask
|
2018-12-17 09:08:40 +08:00
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
internal enum MaskInteraction : int
|
|
|
|
{
|
|
|
|
VisibleInsideMask = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6),
|
|
|
|
VisibleOutsideMask = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6),
|
|
|
|
Custom = -1,
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// SoftMaskable editor.
|
|
|
|
/// </summary>
|
|
|
|
[CustomEditor(typeof(SoftMaskable))]
|
|
|
|
[CanEditMultipleObjects]
|
|
|
|
public class SoftMaskableEditor : Editor
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
private static readonly List<Mask> s_TmpMasks = new List<Mask>();
|
|
|
|
private static GUIContent s_MaskWarning;
|
|
|
|
private SerializedProperty _spMaskInteraction;
|
|
|
|
private bool _custom;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
private MaskInteraction maskInteraction
|
2020-05-11 15:34:22 +08:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
var value = _spMaskInteraction.intValue;
|
2020-05-11 15:34:22 +08:00
|
|
|
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);
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
if (s_MaskWarning == null)
|
|
|
|
{
|
|
|
|
s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"), "This is not a SoftMask component.");
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
private void DrawMaskInteractions()
|
|
|
|
{
|
|
|
|
var softMaskable = target as SoftMaskable;
|
|
|
|
if (softMaskable == null) return;
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
softMaskable.GetComponentsInParent<Mask>(true, s_TmpMasks);
|
|
|
|
s_TmpMasks.RemoveAll(x => !x.enabled);
|
|
|
|
s_TmpMasks.Reverse();
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
maskInteraction = (MaskInteraction) EditorGUILayout.EnumPopup("Mask Interaction", maskInteraction);
|
|
|
|
if (!_custom) return;
|
|
|
|
|
|
|
|
var l = EditorGUIUtility.labelWidth;
|
|
|
|
EditorGUIUtility.labelWidth = 45;
|
|
|
|
|
|
|
|
using (var ccs = new EditorGUI.ChangeCheckScope())
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
var intr0 = DrawMaskInteraction(0);
|
|
|
|
var intr1 = DrawMaskInteraction(1);
|
|
|
|
var intr2 = DrawMaskInteraction(2);
|
|
|
|
var intr3 = DrawMaskInteraction(3);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
if (ccs.changed)
|
|
|
|
{
|
|
|
|
_spMaskInteraction.intValue = (intr0 << 0) + (intr1 << 2) + (intr2 << 4) + (intr3 << 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorGUIUtility.labelWidth = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int DrawMaskInteraction(int layer)
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
var mask = layer < s_TmpMasks.Count ? s_TmpMasks[layer] : null;
|
|
|
|
var intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3);
|
2020-05-11 15:34:22 +08:00
|
|
|
if (!mask)
|
|
|
|
{
|
|
|
|
return (int) intr;
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
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;
|
2020-05-11 15:34:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnInspectorGUI()
|
|
|
|
{
|
|
|
|
base.OnInspectorGUI();
|
|
|
|
|
|
|
|
serializedObject.Update();
|
|
|
|
DrawMaskInteractions();
|
|
|
|
|
|
|
|
serializedObject.ApplyModifiedProperties();
|
|
|
|
|
|
|
|
var current = target as SoftMaskable;
|
2020-06-04 12:43:17 +08:00
|
|
|
if (current == null) return;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
var mask = current.softMask;
|
|
|
|
if (mask) return;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
GUILayout.BeginHorizontal();
|
|
|
|
EditorGUILayout.HelpBox("This is unnecessary SoftMaskable.\nCan't find any SoftMask components above.", MessageType.Warning);
|
|
|
|
if (GUILayout.Button("Remove", GUILayout.Height(40)))
|
2020-05-11 15:34:22 +08:00
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
DestroyImmediate(current);
|
|
|
|
EditorUtils.MarkPrefabDirty();
|
2020-05-11 15:34:22 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
GUILayout.EndHorizontal();
|
2020-05-11 15:34:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|