diff --git a/Samples~/Demo/Unmask_Demo.cs b/Samples~/Demo/Unmask_Demo.cs index 8a5ef22..4d13652 100644 --- a/Samples~/Demo/Unmask_Demo.cs +++ b/Samples~/Demo/Unmask_Demo.cs @@ -1,35 +1,32 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; using UnityEngine.UI; namespace Coffee.UIExtensions.Demos { - public class Unmask_Demo : MonoBehaviour - { - [SerializeField] Button target; - [SerializeField] Unmask unmask; - [SerializeField] Graphic transition; - [SerializeField] Image transitionImage; - [SerializeField] Sprite unity_chan; - [SerializeField] Sprite unity_frame; + public class Unmask_Demo : MonoBehaviour + { + [SerializeField] Unmask unmask = null; + [SerializeField] Graphic transition = null; + [SerializeField] Image transitionImage = null; + [SerializeField] Sprite unity_chan = null; + [SerializeField] Sprite unity_frame = null; - public void AutoFitToButton(bool flag) - { - unmask.fitOnLateUpdate = flag; - } + public void AutoFitToButton(bool flag) + { + unmask.fitOnLateUpdate = flag; + } - public void SetTransitionColor(bool flag) - { - transition.color = flag ? Color.white : Color.black; - } + public void SetTransitionColor(bool flag) + { + transition.color = flag ? Color.white : Color.black; + } - public void SetTransitionImage(bool flag) - { - transitionImage.sprite = flag ? unity_chan : unity_frame; - transitionImage.SetNativeSize(); - var size = transitionImage.rectTransform.rect.size; - transitionImage.rectTransform.sizeDelta = new Vector2(150, size.y / size.x * 150); - } - } + public void SetTransitionImage(bool flag) + { + transitionImage.sprite = flag ? unity_chan : unity_frame; + transitionImage.SetNativeSize(); + var size = transitionImage.rectTransform.rect.size; + transitionImage.rectTransform.sizeDelta = new Vector2(150, size.y / size.x * 150); + } + } } \ No newline at end of file diff --git a/Scripts/Unmask.cs b/Scripts/Unmask.cs index b997cfa..dacd9df 100644 --- a/Scripts/Unmask.cs +++ b/Scripts/Unmask.cs @@ -1,221 +1,223 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; +using UnityEngine.Profiling; using UnityEngine.Rendering; using UnityEngine.UI; namespace Coffee.UIExtensions { - /// - /// Reverse masking for parent Mask component. - /// - [ExecuteInEditMode] - [AddComponentMenu("UI/Unmask/Unmask", 1)] - public class Unmask : MonoBehaviour, IMaterialModifier - { - //################################ - // Constant or Static Members. - //################################ - static readonly Vector2 s_Center = new Vector2(0.5f, 0.5f); + /// + /// Reverse masking for parent Mask component. + /// + [ExecuteInEditMode] + [AddComponentMenu("UI/Unmask/Unmask", 1)] + public class Unmask : MonoBehaviour, IMaterialModifier + { + //################################ + // Constant or Static Members. + //################################ + private static readonly Vector2 s_Center = new Vector2(0.5f, 0.5f); - //################################ - // Serialize Members. - //################################ - [Tooltip("Fit graphic's transform to target transform.")] - [SerializeField] RectTransform m_FitTarget; - [Tooltip("Fit graphic's transform to target transform on LateUpdate every frame.")] - [SerializeField] bool m_FitOnLateUpdate; - [Tooltip ("Unmask affects only for children.")] - [SerializeField] bool m_OnlyForChildren = false; - [Tooltip("Show the graphic that is associated with the unmask render area.")] - [SerializeField] bool m_ShowUnmaskGraphic = false; + //################################ + // Serialize Members. + //################################ + [Tooltip("Fit graphic's transform to target transform.")] + [SerializeField] private RectTransform m_FitTarget; + + [Tooltip("Fit graphic's transform to target transform on LateUpdate every frame.")] + [SerializeField] private bool m_FitOnLateUpdate; + + [Tooltip("Unmask affects only for children.")] + [SerializeField] private bool m_OnlyForChildren = false; + + [Tooltip("Show the graphic that is associated with the unmask render area.")] + [SerializeField] private bool m_ShowUnmaskGraphic = false; - //################################ - // Public Members. - //################################ - /// - /// The graphic associated with the unmask. - /// - public Graphic graphic{ get { return _graphic ?? (_graphic = GetComponent()); } } + //################################ + // Public Members. + //################################ + /// + /// The graphic associated with the unmask. + /// + public MaskableGraphic graphic { get { return _graphic ?? (_graphic = GetComponent()); } } - /// - /// Fit graphic's transform to target transform. - /// - public RectTransform fitTarget - { - get { return m_FitTarget; } - set - { - m_FitTarget = value; - FitTo(m_FitTarget); - } - } + /// + /// Fit graphic's transform to target transform. + /// + public RectTransform fitTarget + { + get { return m_FitTarget; } + set + { + m_FitTarget = value; + FitTo(m_FitTarget); + } + } - /// - /// Fit graphic's transform to target transform on LateUpdate every frame. - /// - public bool fitOnLateUpdate{ get { return m_FitOnLateUpdate; } set { m_FitOnLateUpdate = value; } } + /// + /// Fit graphic's transform to target transform on LateUpdate every frame. + /// + public bool fitOnLateUpdate { get { return m_FitOnLateUpdate; } set { m_FitOnLateUpdate = value; } } - /// - /// Show the graphic that is associated with the unmask render area. - /// - public bool showUnmaskGraphic - { - get { return m_ShowUnmaskGraphic; } - set - { - m_ShowUnmaskGraphic = value; - SetDirty(); - } - } + /// + /// Show the graphic that is associated with the unmask render area. + /// + public bool showUnmaskGraphic + { + get { return m_ShowUnmaskGraphic; } + set + { + m_ShowUnmaskGraphic = value; + SetDirty(); + } + } - /// - /// Unmask affects only for children. - /// - public bool onlyForChildren - { - get { return m_OnlyForChildren; } - set - { - m_OnlyForChildren = value; - SetDirty (); - } - } + /// + /// Unmask affects only for children. + /// + public bool onlyForChildren + { + get { return m_OnlyForChildren; } + set + { + m_OnlyForChildren = value; + SetDirty(); + } + } - /// - /// Perform material modification in this function. - /// - /// Modified material. - /// Configured Material. - public Material GetModifiedMaterial(Material baseMaterial) - { - if (!isActiveAndEnabled) - { - return baseMaterial; - } + /// + /// Perform material modification in this function. + /// + /// Modified material. + /// Configured Material. + public Material GetModifiedMaterial(Material baseMaterial) + { + if (!isActiveAndEnabled) + { + return baseMaterial; + } - Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(transform); - var stencilDepth = MaskUtilities.GetStencilDepth(transform, stopAfter); - var desiredStencilBit = 1 << stencilDepth; + Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(transform); + var stencilDepth = MaskUtilities.GetStencilDepth(transform, stopAfter); + var desiredStencilBit = 1 << stencilDepth; - StencilMaterial.Remove(_unmaskMaterial); - _unmaskMaterial = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Invert, CompareFunction.Equal, m_ShowUnmaskGraphic ? ColorWriteMask.All : (ColorWriteMask)0, desiredStencilBit - 1, (1 << 8) - 1); + StencilMaterial.Remove(_unmaskMaterial); + _unmaskMaterial = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Invert, CompareFunction.Equal, m_ShowUnmaskGraphic ? ColorWriteMask.All : (ColorWriteMask)0, desiredStencilBit - 1, (1 << 8) - 1); - // Unmask affects only for children. - var canvasRenderer = graphic.canvasRenderer; - if (m_OnlyForChildren) - { - StencilMaterial.Remove (_revertUnmaskMaterial); - _revertUnmaskMaterial = StencilMaterial.Add(baseMaterial, (1 << 7), StencilOp.Invert, CompareFunction.Equal, (ColorWriteMask)0, (1 << 7), (1 << 8) - 1); - canvasRenderer.hasPopInstruction = true; - canvasRenderer.popMaterialCount = 1; - canvasRenderer.SetPopMaterial (_revertUnmaskMaterial, 0); - } - else - { - canvasRenderer.hasPopInstruction = false; - canvasRenderer.popMaterialCount = 0; - } + // Unmask affects only for children. + var canvasRenderer = graphic.canvasRenderer; + if (m_OnlyForChildren) + { + StencilMaterial.Remove(_revertUnmaskMaterial); + _revertUnmaskMaterial = StencilMaterial.Add(baseMaterial, (1 << 7), StencilOp.Invert, CompareFunction.Equal, (ColorWriteMask)0, (1 << 7), (1 << 8) - 1); + canvasRenderer.hasPopInstruction = true; + canvasRenderer.popMaterialCount = 1; + canvasRenderer.SetPopMaterial(_revertUnmaskMaterial, 0); + } + else + { + canvasRenderer.hasPopInstruction = false; + canvasRenderer.popMaterialCount = 0; + } - return _unmaskMaterial; - } + return _unmaskMaterial; + } - /// - /// Fit to target transform. - /// - /// Target transform. - public void FitTo(RectTransform target) - { - var rt = transform as RectTransform; + /// + /// Fit to target transform. + /// + /// Target transform. + public void FitTo(RectTransform target) + { + var rt = transform as RectTransform; - rt.pivot = target.pivot; - rt.position = target.position; - rt.rotation = target.rotation; + rt.pivot = target.pivot; + rt.position = target.position; + rt.rotation = target.rotation; - var s1 = target.lossyScale; - var s2 = rt.parent.lossyScale; - rt.localScale = new Vector3(s1.x / s2.x, s1.y / s2.y, s1.z / s2.z); - rt.sizeDelta = target.rect.size; - rt.anchorMax = rt.anchorMin = s_Center; - } + var s1 = target.lossyScale; + var s2 = rt.parent.lossyScale; + rt.localScale = new Vector3(s1.x / s2.x, s1.y / s2.y, s1.z / s2.z); + rt.sizeDelta = target.rect.size; + rt.anchorMax = rt.anchorMin = s_Center; + } - //################################ - // Private Members. - //################################ - Material _unmaskMaterial; - Material _revertUnmaskMaterial; - Graphic _graphic; + //################################ + // Private Members. + //################################ + private Material _unmaskMaterial; + private Material _revertUnmaskMaterial; + private MaskableGraphic _graphic; - /// - /// This function is called when the object becomes enabled and active. - /// - void OnEnable() - { - if (m_FitTarget) - { - FitTo(m_FitTarget); - } - SetDirty(); - } + /// + /// This function is called when the object becomes enabled and active. + /// + private void OnEnable() + { + if (m_FitTarget) + { + FitTo(m_FitTarget); + } + SetDirty(); + } - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - void OnDisable() - { - StencilMaterial.Remove (_unmaskMaterial); - StencilMaterial.Remove (_revertUnmaskMaterial); - _unmaskMaterial = null; - _revertUnmaskMaterial = null; + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + private void OnDisable() + { + StencilMaterial.Remove(_unmaskMaterial); + StencilMaterial.Remove(_revertUnmaskMaterial); + _unmaskMaterial = null; + _revertUnmaskMaterial = null; - if (graphic) - { - var canvasRenderer = graphic.canvasRenderer; - canvasRenderer.hasPopInstruction = false; - canvasRenderer.popMaterialCount = 0; - graphic.SetMaterialDirty(); - } - SetDirty (); - } + if (graphic) + { + var canvasRenderer = graphic.canvasRenderer; + canvasRenderer.hasPopInstruction = false; + canvasRenderer.popMaterialCount = 0; + graphic.SetMaterialDirty(); + } + SetDirty(); + } - /// - /// LateUpdate is called every frame, if the Behaviour is enabled. - /// - void LateUpdate() - { + /// + /// LateUpdate is called every frame, if the Behaviour is enabled. + /// + private void LateUpdate() + { #if UNITY_EDITOR - if (m_FitTarget && (m_FitOnLateUpdate || !Application.isPlaying)) + if (m_FitTarget && (m_FitOnLateUpdate || !Application.isPlaying)) #else if (m_FitTarget && m_FitOnLateUpdate) #endif - { - FitTo(m_FitTarget); - } - } + { + FitTo(m_FitTarget); + } + } #if UNITY_EDITOR - /// - /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). - /// - void OnValidate() - { - SetDirty(); - } + /// + /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). + /// + private void OnValidate() + { + SetDirty(); + } #endif - /// - /// Mark the graphic as dirty. - /// - void SetDirty() - { - if (graphic) - { - graphic.SetMaterialDirty(); - } - } - } + /// + /// Mark the graphic as dirty. + /// + void SetDirty() + { + if (graphic) + { + graphic.SetMaterialDirty(); + } + } + } } diff --git a/Scripts/UnmaskRaycastFilter.cs b/Scripts/UnmaskRaycastFilter.cs index 63af057..d2e75ab 100644 --- a/Scripts/UnmaskRaycastFilter.cs +++ b/Scripts/UnmaskRaycastFilter.cs @@ -1,46 +1,42 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.UI; - +using UnityEngine; namespace Coffee.UIExtensions { - /// - /// Unmask Raycast Filter. - /// The ray passes through the unmasked rectangle. - /// - [AddComponentMenu("UI/Unmask/UnmaskRaycastFilter", 2)] - public class UnmaskRaycastFilter : MonoBehaviour, ICanvasRaycastFilter - { - //################################ - // Serialize Members. - //################################ - [Tooltip("Target unmask component. The ray passes through the unmasked rectangle.")] - [SerializeField] Unmask m_TargetUnmask; + /// + /// Unmask Raycast Filter. + /// The ray passes through the unmasked rectangle. + /// + [AddComponentMenu("UI/Unmask/UnmaskRaycastFilter", 2)] + public class UnmaskRaycastFilter : MonoBehaviour, ICanvasRaycastFilter + { + //################################ + // Serialize Members. + //################################ + [Tooltip("Target unmask component. The ray passes through the unmasked rectangle.")] + [SerializeField] private Unmask m_TargetUnmask; - //################################ - // Public Members. - //################################ - /// - /// Target unmask component. Ray through the unmasked rectangle. - /// - public Unmask targetUnmask{ get { return m_TargetUnmask; } set { m_TargetUnmask = value; } } + //################################ + // Public Members. + //################################ + /// + /// Target unmask component. Ray through the unmasked rectangle. + /// + public Unmask targetUnmask { get { return m_TargetUnmask; } set { m_TargetUnmask = value; } } - /// - /// Given a point and a camera is the raycast valid. - /// - /// Valid. - /// Screen position. - /// Raycast camera. - public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) - { - // Skip if deactived. - if (!isActiveAndEnabled || !m_TargetUnmask || !m_TargetUnmask.isActiveAndEnabled) - { - return true; - } + /// + /// Given a point and a camera is the raycast valid. + /// + /// Valid. + /// Screen position. + /// Raycast camera. + public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) + { + // Skip if deactived. + if (!isActiveAndEnabled || !m_TargetUnmask || !m_TargetUnmask.isActiveAndEnabled) + { + return true; + } // check inside if (eventCamera) @@ -51,18 +47,18 @@ namespace Coffee.UIExtensions { return !RectTransformUtility.RectangleContainsScreenPoint((m_TargetUnmask.transform as RectTransform), sp); } - } + } - //################################ - // Private Members. - //################################ + //################################ + // Private Members. + //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - void OnEnable() - { - } - } + /// + /// This function is called when the object becomes enabled and active. + /// + void OnEnable() + { + } + } } \ No newline at end of file