diff --git a/Packages/SoftMaskForUGUI/Scripts/Editor/EditorUtils.cs b/Packages/SoftMaskForUGUI/Scripts/Editor/EditorUtils.cs index d41f9f7..6a8c097 100644 --- a/Packages/SoftMaskForUGUI/Scripts/Editor/EditorUtils.cs +++ b/Packages/SoftMaskForUGUI/Scripts/Editor/EditorUtils.cs @@ -5,7 +5,6 @@ using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; - namespace Coffee.UISoftMask { internal static class EditorUtils @@ -36,7 +35,7 @@ namespace Coffee.UISoftMask var so = new SerializedObject(target); so.Update(); - bool oldEnable = target.enabled; + var oldEnable = target.enabled; target.enabled = false; // Find MonoScript of the specified component. diff --git a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs index 8116ea0..acbe89e 100644 --- a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs +++ b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs @@ -14,9 +14,9 @@ namespace Coffee.UISoftMask [CanEditMultipleObjects] public class SoftMaskEditor : Editor { - const string k_PrefsPreview = "SoftMaskEditor_Preview"; - static readonly List s_Graphics = new List(); - static bool s_Preview; + private const string k_PrefsPreview = "SoftMaskEditor_Preview"; + private static readonly List s_Graphics = new List(); + private static bool s_Preview; private void OnEnable() { @@ -35,9 +35,7 @@ namespace Coffee.UISoftMask if (0 < fixTargets.Count) { GUILayout.BeginHorizontal(); - EditorGUILayout.HelpBox( - "There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", - MessageType.Warning); + EditorGUILayout.HelpBox("There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning); GUILayout.BeginVertical(); if (GUILayout.Button("Fix")) { @@ -69,8 +67,8 @@ namespace Coffee.UISoftMask if (s_Preview) { var tex = current.softMaskBuffer; - var width = tex.width * 64 / tex.height; - EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, 64), tex, null, ScaleMode.ScaleToFit); + var width = tex.width * 128 / tex.height; + EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, 128), tex, null, ScaleMode.ScaleToFit); Repaint(); } @@ -81,25 +79,25 @@ namespace Coffee.UISoftMask //%%%% Context menu for editor %%%% [MenuItem("CONTEXT/Mask/Convert To SoftMask", true)] - static bool _ConvertToSoftMask(MenuCommand command) + private static bool _ConvertToSoftMask(MenuCommand command) { return EditorUtils.CanConvertTo(command.context); } [MenuItem("CONTEXT/Mask/Convert To SoftMask", false)] - static void ConvertToSoftMask(MenuCommand command) + private static void ConvertToSoftMask(MenuCommand command) { EditorUtils.ConvertTo(command.context); } [MenuItem("CONTEXT/Mask/Convert To Mask", true)] - static bool _ConvertToMask(MenuCommand command) + private static bool _ConvertToMask(MenuCommand command) { return EditorUtils.CanConvertTo(command.context); } [MenuItem("CONTEXT/Mask/Convert To Mask", false)] - static void ConvertToMask(MenuCommand command) + private static void ConvertToMask(MenuCommand command) { EditorUtils.ConvertTo(command.context); } diff --git a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs index 54272f9..048ba26 100644 --- a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs +++ b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskableEditor.cs @@ -3,14 +3,17 @@ using UnityEngine; using UnityEngine.UI; using UnityEditor; using System.Linq; -using System; -using System.Reflection; -using Object = UnityEngine.Object; using MaskIntr = UnityEngine.SpriteMaskInteraction; -using System.IO; 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, + } + /// /// SoftMaskable editor. /// @@ -18,18 +21,16 @@ namespace Coffee.UISoftMask [CanEditMultipleObjects] public class SoftMaskableEditor : Editor { - public enum MaskInteraction : int - { - VisibleInsideMask = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6), - VisibleOutsideMask = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6), - Custom = -1, - } + private static readonly List s_TmpMasks = new List(); + private static GUIContent s_MaskWarning; + private SerializedProperty _spMaskInteraction; + private bool _custom; - MaskInteraction maskInteraction + private MaskInteraction maskInteraction { get { - int value = _spMaskInteraction.intValue; + var value = _spMaskInteraction.intValue; return _custom ? MaskInteraction.Custom : System.Enum.IsDefined(typeof(MaskInteraction), value) @@ -46,31 +47,26 @@ namespace Coffee.UISoftMask } } - bool _custom = false; - - static readonly List s_Graphics = new List(); - SerializedProperty _spMaskInteraction; - List tmpMasks = new List(); - static GUIContent s_MaskWarning; - private void OnEnable() { _spMaskInteraction = serializedObject.FindProperty("m_MaskInteraction"); _custom = (maskInteraction == MaskInteraction.Custom); - s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"), - "This is not a SoftMask component."); - } + 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(true, tmpMasks); - tmpMasks.RemoveAll(x => !x.enabled); - tmpMasks.Reverse(); + softMaskable.GetComponentsInParent(true, s_TmpMasks); + s_TmpMasks.RemoveAll(x => !x.enabled); + s_TmpMasks.Reverse(); maskInteraction = (MaskInteraction) EditorGUILayout.EnumPopup("Mask Interaction", maskInteraction); if (!_custom) return; @@ -80,10 +76,10 @@ namespace Coffee.UISoftMask using (var ccs = new EditorGUI.ChangeCheckScope()) { - int intr0 = DrawMaskInteraction(0); - int intr1 = DrawMaskInteraction(1); - int intr2 = DrawMaskInteraction(2); - int intr3 = DrawMaskInteraction(3); + var intr0 = DrawMaskInteraction(0); + var intr1 = DrawMaskInteraction(1); + var intr2 = DrawMaskInteraction(2); + var intr3 = DrawMaskInteraction(3); if (ccs.changed) { @@ -94,24 +90,24 @@ namespace Coffee.UISoftMask EditorGUIUtility.labelWidth = l; } - private int DrawMaskInteraction(int layer) { - Mask mask = layer < tmpMasks.Count ? tmpMasks[layer] : null; - MaskIntr intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3); + var mask = layer < s_TmpMasks.Count ? s_TmpMasks[layer] : null; + var intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3); if (!mask) { return (int) intr; } - using (new EditorGUILayout.HorizontalScope()) - { - 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); - return (int) (MaskIntr) EditorGUILayout.EnumPopup(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() @@ -124,61 +120,20 @@ namespace Coffee.UISoftMask serializedObject.ApplyModifiedProperties(); var current = target as SoftMaskable; + if (current == null) return; - current.GetComponentsInChildren(true, s_Graphics); - var fixTargets = s_Graphics.Where(x => - x.gameObject != current.gameObject && !x.GetComponent() && - (!x.GetComponent() || x.GetComponent().showMaskGraphic)).ToList(); - if (0 < fixTargets.Count) + 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))) { - GUILayout.BeginHorizontal(); - EditorGUILayout.HelpBox( - "There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", - MessageType.Warning); - GUILayout.BeginVertical(); - if (GUILayout.Button("Fix")) - { - foreach (var p in fixTargets) - { - p.gameObject.AddComponent(); - } - } - - if (GUILayout.Button("Ping")) - { - EditorGUIUtility.PingObject(fixTargets[0]); - } - - GUILayout.EndVertical(); - GUILayout.EndHorizontal(); + DestroyImmediate(current); + EditorUtils.MarkPrefabDirty(); } - if (!DetectMask(current.transform.parent)) - { - 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(); - } - } - - static bool DetectMask(Transform transform) - { - while (transform) - { - if (transform.GetComponent()) return true; - - transform = transform.parent; - } - - return false; + GUILayout.EndHorizontal(); } } } diff --git a/Packages/SoftMaskForUGUI/Scripts/GraphicConnector.cs b/Packages/SoftMaskForUGUI/Scripts/GraphicConnector.cs index a437335..1627f5f 100644 --- a/Packages/SoftMaskForUGUI/Scripts/GraphicConnector.cs +++ b/Packages/SoftMaskForUGUI/Scripts/GraphicConnector.cs @@ -17,9 +17,20 @@ namespace Coffee.UISoftMask GraphicConnector.FindConnector(graphic).SetMaterialDirty(graphic); } - public static Shader FindEffectShader(this Graphic graphic) + public static T GetComponentInParentEx(this Component component, bool includeInactive = false) where T : MonoBehaviour { - return GraphicConnector.FindConnector(graphic).FindEffectShader(graphic); + if (!component) return null; + var trans = component.transform; + + while (trans) + { + var c = trans.GetComponent(); + if (c && (includeInactive || c.isActiveAndEnabled)) return c; + + trans = trans.parent; + } + + return null; } } @@ -27,10 +38,7 @@ namespace Coffee.UISoftMask public class GraphicConnector { private static readonly List s_Connectors = new List(); - - private static readonly Dictionary s_ConnectorMap = - new Dictionary(); - + private static readonly Dictionary s_ConnectorMap = new Dictionary(); private static readonly GraphicConnector s_EmptyConnector = new GraphicConnector(); #if UNITY_EDITOR @@ -75,15 +83,6 @@ namespace Coffee.UISoftMask get { return -1; } } - - /// - /// Find effect shader. - /// - public virtual Shader FindEffectShader(Graphic graphic) - { - return Shader.Find("Hidden/UI/SoftMaskable"); - } - /// /// The connector is valid for the component. /// diff --git a/Packages/SoftMaskForUGUI/Scripts/MaterialCache.cs b/Packages/SoftMaskForUGUI/Scripts/MaterialCache.cs index 055abb2..47b80e4 100644 --- a/Packages/SoftMaskForUGUI/Scripts/MaterialCache.cs +++ b/Packages/SoftMaskForUGUI/Scripts/MaterialCache.cs @@ -1,42 +1,44 @@ using System.Collections.Generic; using System; using UnityEngine; -using UnityEngine.UI; namespace Coffee.UISoftMask { - internal class MaterialCache + internal class MaterialEntry { - public delegate void ModifyAction(Material material, Graphic graphic); + public Material material; + public int referenceCount; - static Dictionary materialMap = new Dictionary(); - - private class MaterialEntry + public void Release() { - public Material material; - public int referenceCount; - - public void Release() + if (material) { - if (material) - { +#if UNITY_EDITOR + if (!Application.isPlaying) UnityEngine.Object.DestroyImmediate(material, false); - } - - material = null; + else +#endif + UnityEngine.Object.Destroy(material); } + + material = null; } + } + + internal static class MaterialCache + { + static readonly Dictionary s_MaterialMap = new Dictionary(); #if UNITY_EDITOR [UnityEditor.InitializeOnLoadMethod] private static void ClearCache() { - foreach (var entry in materialMap.Values) + foreach (var entry in s_MaterialMap.Values) { entry.Release(); } - materialMap.Clear(); + s_MaterialMap.Clear(); } #endif @@ -45,7 +47,7 @@ namespace Coffee.UISoftMask if (!hash.isValid) return null; MaterialEntry entry; - if (!materialMap.TryGetValue(hash, out entry)) + if (!s_MaterialMap.TryGetValue(hash, out entry)) { entry = new MaterialEntry() { @@ -56,7 +58,7 @@ namespace Coffee.UISoftMask }; onModify(entry.material); - materialMap.Add(hash, entry); + s_MaterialMap.Add(hash, entry); } entry.referenceCount++; @@ -67,13 +69,13 @@ namespace Coffee.UISoftMask public static void Unregister(Hash128 hash) { MaterialEntry entry; - if (!hash.isValid || !materialMap.TryGetValue(hash, out entry)) return; + if (!hash.isValid || !s_MaterialMap.TryGetValue(hash, out entry)) return; //Debug.LogFormat("Unregister: {0}, {1}", hash, entry.referenceCount -1); if (--entry.referenceCount > 0) return; entry.Release(); - materialMap.Remove(hash); + s_MaterialMap.Remove(hash); //Debug.LogFormat("Unregister: Release Emtry: {0}, {1} (Total: {2})", hash, entry.referenceCount, materialMap.Count); } } diff --git a/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs b/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs index 4c38ebb..23cf6c0 100644 --- a/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -25,7 +25,7 @@ namespace Coffee.UISoftMask x8 = 8, } - static readonly List[] s_TmpSoftMasks = new List[] + private static readonly List[] s_TmpSoftMasks = new List[] { new List(), new List(), @@ -33,7 +33,7 @@ namespace Coffee.UISoftMask new List(), }; - static readonly Color[] s_ClearColors = new Color[] + private static readonly Color[] s_ClearColors = new Color[] { new Color(0, 0, 0, 0), new Color(1, 0, 0, 0), @@ -41,50 +41,46 @@ namespace Coffee.UISoftMask new Color(1, 1, 1, 0), }; - static bool s_UVStartsAtTop; - - static Shader s_SoftMaskShader; - static Texture2D s_ReadTexture; - static readonly List s_ActiveSoftMasks = new List(); - static readonly List s_TempRelatables = new List(); - static readonly Dictionary s_previousViewProjectionMatrices = new Dictionary(); - static readonly Dictionary s_nowViewProjectionMatrices = new Dictionary(); - static int s_StencilCompId; - static int s_ColorMaskId; - static int s_MainTexId; - static int s_SoftnessId; - static int s_GameVPId; - static int s_GameTVPId; - static int s_Alpha; - MaterialPropertyBlock _mpb; - CommandBuffer _cb; - Material _material; - RenderTexture _softMaskBuffer; - int _stencilDepth; - Mesh _mesh; - SoftMask _parent; - readonly List _children = new List(); - bool _hasChanged = false; - bool _hasStencilStateChanged = false; + private static bool s_UVStartsAtTop; + private static Shader s_SoftMaskShader; + private static Texture2D s_ReadTexture; + private static readonly List s_ActiveSoftMasks = new List(); + private static readonly List s_TempRelatables = new List(); + private static readonly Dictionary s_PreviousViewProjectionMatrices = new Dictionary(); + private static readonly Dictionary s_NowViewProjectionMatrices = new Dictionary(); + private static int s_StencilCompId; + private static int s_ColorMaskId; + private static int s_MainTexId; + private static int s_SoftnessId; + private static int s_GameVPId; + private static int s_GameTVPId; + private static int s_Alpha; + private MaterialPropertyBlock _mpb; + private CommandBuffer _cb; + private Material _material; + private RenderTexture _softMaskBuffer; + private int _stencilDepth; + private Mesh _mesh; + private SoftMask _parent; + private readonly List _children = new List(); + private bool _hasChanged = false; + private bool _hasStencilStateChanged = false; - [Tooltip("The desampling rate for soft mask buffer.")] [SerializeField] - DesamplingRate m_DesamplingRate = DesamplingRate.None; + [SerializeField, Tooltip("The desampling rate for soft mask buffer.")] + private DesamplingRate m_DesamplingRate = DesamplingRate.None; - [Tooltip( - "The value used by the soft mask to select the area of influence defined over the soft mask's graphic.")] - [SerializeField] - [Range(0.01f, 1)] - float m_Softness = 1; + [SerializeField, Range(0.01f, 1), Tooltip("The value used by the soft mask to select the area of influence defined over the soft mask's graphic.")] + private float m_Softness = 1; - [Tooltip("The transparency of the whole masked graphic.")] [SerializeField] [Range(0f, 1f)] - float m_Alpha = 1; + [SerializeField, Range(0f, 1f), Tooltip("The transparency of the whole masked graphic.")] + private float m_Alpha = 1; - [Tooltip("Should the soft mask ignore parent soft masks?")] [SerializeField] - bool m_IgnoreParent = false; + [SerializeField, Tooltip("Should the soft mask ignore parent soft masks?")] + private bool m_IgnoreParent = false; - [Tooltip("Is the soft mask a part of parent soft mask?")] [SerializeField] - bool m_PartOfParent = false; + [SerializeField, Tooltip("Is the soft mask a part of parent soft mask?")] + private bool m_PartOfParent = false; /// @@ -336,7 +332,7 @@ namespace Coffee.UISoftMask _mpb = new MaterialPropertyBlock(); _cb = new CommandBuffer(); - graphic.SetVerticesDirty(); + graphic.SetVerticesDirtyEx(); base.OnEnable(); _hasStencilStateChanged = false; @@ -411,7 +407,7 @@ namespace Coffee.UISoftMask /// protected override void OnValidate() { - graphic.SetMaterialDirty(); + graphic.SetMaterialDirtyEx(); OnTransformParentChanged(); base.OnValidate(); _hasStencilStateChanged = false; @@ -441,8 +437,8 @@ namespace Coffee.UISoftMask var nowVP = cam.projectionMatrix * cam.worldToCameraMatrix; var previousVP = default(Matrix4x4); var id = cam.GetInstanceID(); - s_previousViewProjectionMatrices.TryGetValue(id, out previousVP); - s_nowViewProjectionMatrices[id] = nowVP; + s_PreviousViewProjectionMatrices.TryGetValue(id, out previousVP); + s_NowViewProjectionMatrices[id] = nowVP; if (previousVP != nowVP) { @@ -478,13 +474,13 @@ namespace Coffee.UISoftMask MaskUtilities.NotifyStencilStateChanged(sm); } - s_previousViewProjectionMatrices.Clear(); - foreach (var id in s_nowViewProjectionMatrices.Keys) + s_PreviousViewProjectionMatrices.Clear(); + foreach (var id in s_NowViewProjectionMatrices.Keys) { - s_previousViewProjectionMatrices[id] = s_nowViewProjectionMatrices[id]; + s_PreviousViewProjectionMatrices[id] = s_NowViewProjectionMatrices[id]; } - s_nowViewProjectionMatrices.Clear(); + s_NowViewProjectionMatrices.Clear(); } /// diff --git a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs index 2be2cbc..fe902d0 100755 --- a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs +++ b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs @@ -15,40 +15,37 @@ namespace Coffee.UISoftMask #else [ExecuteInEditMode] # endif + [RequireComponent(typeof(Graphic))] public class SoftMaskable : MonoBehaviour, IMaterialModifier, ICanvasRaycastFilter #if UNITY_EDITOR , ISerializationCallbackReceiver # endif { - const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6); - const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6); - static readonly Hash128 k_InvalidHash = new Hash128(); + private const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6); + private const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6); + private static readonly Hash128 k_InvalidHash = new Hash128(); - static int s_SoftMaskTexId; - static int s_StencilCompId; - static int s_MaskInteractionId; - static List s_ActiveSoftMaskables; - static int[] s_Interactions = new int[4]; + private static int s_SoftMaskTexId; + private static int s_StencilCompId; + private static int s_MaskInteractionId; + private static List s_ActiveSoftMaskables; + private static int[] s_Interactions = new int[4]; - [Tooltip("The graphic will be visible only in areas where no mask is present.")] - [System.Obsolete] - [HideInInspector] - [SerializeField] - bool m_Inverse = false; + [SerializeField, HideInInspector, System.Obsolete] + private bool m_Inverse; - [Tooltip("The interaction for each masks.")] [HideInInspector] [SerializeField] - int m_MaskInteraction = kVisibleInside; + [SerializeField, Tooltip("The interaction for each masks."), HideInInspector] + private int m_MaskInteraction = kVisibleInside; - [Tooltip("Use stencil to mask.")] [SerializeField] - bool m_UseStencil = false; + [SerializeField, Tooltip("Use stencil to mask.")] + private bool m_UseStencil; - [Tooltip("Use soft-masked raycast target.\n\nNote: This option is expensive.")] [SerializeField] - bool m_RaycastFilter = false; + [SerializeField, Tooltip("Use soft-masked raycast target.\n\nNote: This option is expensive.")] + private bool m_RaycastFilter; - Graphic _graphic = null; - SoftMask _softMask = null; - Material _maskMaterial = null; - Hash128 _effectMaterialHash; + private Graphic _graphic; + private SoftMask _softMask; + private Hash128 _effectMaterialHash; /// /// The graphic will be visible only in areas where no mask is present. @@ -82,6 +79,11 @@ namespace Coffee.UISoftMask get { return _graphic ? _graphic : _graphic = GetComponent(); } } + public SoftMask softMask + { + get { return _softMask ? _softMask : _softMask = this.GetComponentInParentEx(); } + } + /// /// Perform material modification in this function. /// @@ -98,27 +100,13 @@ namespace Coffee.UISoftMask // If this component is disabled, the material is returned as is. if (!isActiveAndEnabled) return baseMaterial; - // Find the nearest parent softmask. - var parentTransform = transform.parent; - while (parentTransform) - { - var sm = parentTransform.GetComponent(); - if (sm && sm.enabled) - { - _softMask = sm; - break; - } - - parentTransform = parentTransform.parent; - } - // If the parents do not have a soft mask component, the material is returned as is. - if (!_softMask) return baseMaterial; + if (!softMask) return baseMaterial; // Generate soft maskable material. _effectMaterialHash = new Hash128( (uint) baseMaterial.GetInstanceID(), - (uint) _softMask.GetInstanceID(), + (uint) softMask.GetInstanceID(), (uint) m_MaskInteraction, (uint) (m_UseStencil ? 1 : 0) ); @@ -130,7 +118,7 @@ namespace Coffee.UISoftMask #if UNITY_EDITOR mat.EnableKeyword("SOFTMASK_EDITOR"); #endif - mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer); + mat.SetTexture(s_SoftMaskTexId, softMask.softMaskBuffer); mat.SetInt(s_StencilCompId, m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always); mat.SetVector(s_MaskInteractionId, new Vector4( @@ -140,7 +128,6 @@ namespace Coffee.UISoftMask ((m_MaskInteraction >> 6) & 0x3) )); }); - _maskMaterial = modifiedMaterial; return modifiedMaterial; } @@ -153,21 +140,21 @@ namespace Coffee.UISoftMask /// Raycast camera. bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { - if (!isActiveAndEnabled || !_softMask) + if (!isActiveAndEnabled || !softMask) return true; if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera)) return false; if (!m_RaycastFilter) return true; - var sm = _softMask; + var sm = softMask; for (var i = 0; i < 4; i++) { s_Interactions[i] = sm ? ((m_MaskInteraction >> i * 2) & 0x3) : 0; sm = sm ? sm.parent : null; } - return _softMask.IsRaycastLocationValid(sp, eventCamera, graphic, s_Interactions); + return softMask.IsRaycastLocationValid(sp, eventCamera, graphic, s_Interactions); } /// @@ -243,7 +230,7 @@ namespace Coffee.UISoftMask if (m_Inverse) { m_Inverse = false; - m_MaskInteraction = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6); + m_MaskInteraction = kVisibleOutside; } #pragma warning restore 0612