style: code format

main
mob-sakai 2022-02-18 03:48:23 +09:00
parent 1b5588f418
commit 56dfb3c96e
3 changed files with 254 additions and 259 deletions

View File

@ -1,35 +1,32 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
namespace Coffee.UIExtensions.Demos namespace Coffee.UIExtensions.Demos
{ {
public class Unmask_Demo : MonoBehaviour public class Unmask_Demo : MonoBehaviour
{ {
[SerializeField] Button target; [SerializeField] Unmask unmask = null;
[SerializeField] Unmask unmask; [SerializeField] Graphic transition = null;
[SerializeField] Graphic transition; [SerializeField] Image transitionImage = null;
[SerializeField] Image transitionImage; [SerializeField] Sprite unity_chan = null;
[SerializeField] Sprite unity_chan; [SerializeField] Sprite unity_frame = null;
[SerializeField] Sprite unity_frame;
public void AutoFitToButton(bool flag) public void AutoFitToButton(bool flag)
{ {
unmask.fitOnLateUpdate = flag; unmask.fitOnLateUpdate = flag;
} }
public void SetTransitionColor(bool flag) public void SetTransitionColor(bool flag)
{ {
transition.color = flag ? Color.white : Color.black; transition.color = flag ? Color.white : Color.black;
} }
public void SetTransitionImage(bool flag) public void SetTransitionImage(bool flag)
{ {
transitionImage.sprite = flag ? unity_chan : unity_frame; transitionImage.sprite = flag ? unity_chan : unity_frame;
transitionImage.SetNativeSize(); transitionImage.SetNativeSize();
var size = transitionImage.rectTransform.rect.size; var size = transitionImage.rectTransform.rect.size;
transitionImage.rectTransform.sizeDelta = new Vector2(150, size.y / size.x * 150); transitionImage.rectTransform.sizeDelta = new Vector2(150, size.y / size.x * 150);
} }
} }
} }

View File

@ -1,221 +1,223 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic; using UnityEngine.Profiling;
using UnityEngine;
using UnityEngine.Rendering; using UnityEngine.Rendering;
using UnityEngine.UI; using UnityEngine.UI;
namespace Coffee.UIExtensions namespace Coffee.UIExtensions
{ {
/// <summary> /// <summary>
/// Reverse masking for parent Mask component. /// Reverse masking for parent Mask component.
/// </summary> /// </summary>
[ExecuteInEditMode] [ExecuteInEditMode]
[AddComponentMenu("UI/Unmask/Unmask", 1)] [AddComponentMenu("UI/Unmask/Unmask", 1)]
public class Unmask : MonoBehaviour, IMaterialModifier public class Unmask : MonoBehaviour, IMaterialModifier
{ {
//################################ //################################
// Constant or Static Members. // Constant or Static Members.
//################################ //################################
static readonly Vector2 s_Center = new Vector2(0.5f, 0.5f); private static readonly Vector2 s_Center = new Vector2(0.5f, 0.5f);
//################################ //################################
// Serialize Members. // Serialize Members.
//################################ //################################
[Tooltip("Fit graphic's transform to target transform.")] [Tooltip("Fit graphic's transform to target transform.")]
[SerializeField] RectTransform m_FitTarget; [SerializeField] private RectTransform m_FitTarget;
[Tooltip("Fit graphic's transform to target transform on LateUpdate every frame.")]
[SerializeField] bool m_FitOnLateUpdate; [Tooltip("Fit graphic's transform to target transform on LateUpdate every frame.")]
[Tooltip ("Unmask affects only for children.")] [SerializeField] private bool m_FitOnLateUpdate;
[SerializeField] bool m_OnlyForChildren = false;
[Tooltip("Show the graphic that is associated with the unmask render area.")] [Tooltip("Unmask affects only for children.")]
[SerializeField] bool m_ShowUnmaskGraphic = false; [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. // Public Members.
//################################ //################################
/// <summary> /// <summary>
/// The graphic associated with the unmask. /// The graphic associated with the unmask.
/// </summary> /// </summary>
public Graphic graphic{ get { return _graphic ?? (_graphic = GetComponent<Graphic>()); } } public MaskableGraphic graphic { get { return _graphic ?? (_graphic = GetComponent<MaskableGraphic>()); } }
/// <summary> /// <summary>
/// Fit graphic's transform to target transform. /// Fit graphic's transform to target transform.
/// </summary> /// </summary>
public RectTransform fitTarget public RectTransform fitTarget
{ {
get { return m_FitTarget; } get { return m_FitTarget; }
set set
{ {
m_FitTarget = value; m_FitTarget = value;
FitTo(m_FitTarget); FitTo(m_FitTarget);
} }
} }
/// <summary> /// <summary>
/// Fit graphic's transform to target transform on LateUpdate every frame. /// Fit graphic's transform to target transform on LateUpdate every frame.
/// </summary> /// </summary>
public bool fitOnLateUpdate{ get { return m_FitOnLateUpdate; } set { m_FitOnLateUpdate = value; } } public bool fitOnLateUpdate { get { return m_FitOnLateUpdate; } set { m_FitOnLateUpdate = value; } }
/// <summary> /// <summary>
/// Show the graphic that is associated with the unmask render area. /// Show the graphic that is associated with the unmask render area.
/// </summary> /// </summary>
public bool showUnmaskGraphic public bool showUnmaskGraphic
{ {
get { return m_ShowUnmaskGraphic; } get { return m_ShowUnmaskGraphic; }
set set
{ {
m_ShowUnmaskGraphic = value; m_ShowUnmaskGraphic = value;
SetDirty(); SetDirty();
} }
} }
/// <summary> /// <summary>
/// Unmask affects only for children. /// Unmask affects only for children.
/// </summary> /// </summary>
public bool onlyForChildren public bool onlyForChildren
{ {
get { return m_OnlyForChildren; } get { return m_OnlyForChildren; }
set set
{ {
m_OnlyForChildren = value; m_OnlyForChildren = value;
SetDirty (); SetDirty();
} }
} }
/// <summary> /// <summary>
/// Perform material modification in this function. /// Perform material modification in this function.
/// </summary> /// </summary>
/// <returns>Modified material.</returns> /// <returns>Modified material.</returns>
/// <param name="baseMaterial">Configured Material.</param> /// <param name="baseMaterial">Configured Material.</param>
public Material GetModifiedMaterial(Material baseMaterial) public Material GetModifiedMaterial(Material baseMaterial)
{ {
if (!isActiveAndEnabled) if (!isActiveAndEnabled)
{ {
return baseMaterial; return baseMaterial;
} }
Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(transform); Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(transform);
var stencilDepth = MaskUtilities.GetStencilDepth(transform, stopAfter); var stencilDepth = MaskUtilities.GetStencilDepth(transform, stopAfter);
var desiredStencilBit = 1 << stencilDepth; var desiredStencilBit = 1 << stencilDepth;
StencilMaterial.Remove(_unmaskMaterial); StencilMaterial.Remove(_unmaskMaterial);
_unmaskMaterial = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Invert, CompareFunction.Equal, m_ShowUnmaskGraphic ? ColorWriteMask.All : (ColorWriteMask)0, desiredStencilBit - 1, (1 << 8) - 1); _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. // Unmask affects only for children.
var canvasRenderer = graphic.canvasRenderer; var canvasRenderer = graphic.canvasRenderer;
if (m_OnlyForChildren) if (m_OnlyForChildren)
{ {
StencilMaterial.Remove (_revertUnmaskMaterial); StencilMaterial.Remove(_revertUnmaskMaterial);
_revertUnmaskMaterial = StencilMaterial.Add(baseMaterial, (1 << 7), StencilOp.Invert, CompareFunction.Equal, (ColorWriteMask)0, (1 << 7), (1 << 8) - 1); _revertUnmaskMaterial = StencilMaterial.Add(baseMaterial, (1 << 7), StencilOp.Invert, CompareFunction.Equal, (ColorWriteMask)0, (1 << 7), (1 << 8) - 1);
canvasRenderer.hasPopInstruction = true; canvasRenderer.hasPopInstruction = true;
canvasRenderer.popMaterialCount = 1; canvasRenderer.popMaterialCount = 1;
canvasRenderer.SetPopMaterial (_revertUnmaskMaterial, 0); canvasRenderer.SetPopMaterial(_revertUnmaskMaterial, 0);
} }
else else
{ {
canvasRenderer.hasPopInstruction = false; canvasRenderer.hasPopInstruction = false;
canvasRenderer.popMaterialCount = 0; canvasRenderer.popMaterialCount = 0;
} }
return _unmaskMaterial; return _unmaskMaterial;
} }
/// <summary> /// <summary>
/// Fit to target transform. /// Fit to target transform.
/// </summary> /// </summary>
/// <param name="target">Target transform.</param> /// <param name="target">Target transform.</param>
public void FitTo(RectTransform target) public void FitTo(RectTransform target)
{ {
var rt = transform as RectTransform; var rt = transform as RectTransform;
rt.pivot = target.pivot; rt.pivot = target.pivot;
rt.position = target.position; rt.position = target.position;
rt.rotation = target.rotation; rt.rotation = target.rotation;
var s1 = target.lossyScale; var s1 = target.lossyScale;
var s2 = rt.parent.lossyScale; var s2 = rt.parent.lossyScale;
rt.localScale = new Vector3(s1.x / s2.x, s1.y / s2.y, s1.z / s2.z); rt.localScale = new Vector3(s1.x / s2.x, s1.y / s2.y, s1.z / s2.z);
rt.sizeDelta = target.rect.size; rt.sizeDelta = target.rect.size;
rt.anchorMax = rt.anchorMin = s_Center; rt.anchorMax = rt.anchorMin = s_Center;
} }
//################################ //################################
// Private Members. // Private Members.
//################################ //################################
Material _unmaskMaterial; private Material _unmaskMaterial;
Material _revertUnmaskMaterial; private Material _revertUnmaskMaterial;
Graphic _graphic; private MaskableGraphic _graphic;
/// <summary> /// <summary>
/// This function is called when the object becomes enabled and active. /// This function is called when the object becomes enabled and active.
/// </summary> /// </summary>
void OnEnable() private void OnEnable()
{ {
if (m_FitTarget) if (m_FitTarget)
{ {
FitTo(m_FitTarget); FitTo(m_FitTarget);
} }
SetDirty(); SetDirty();
} }
/// <summary> /// <summary>
/// This function is called when the behaviour becomes disabled () or inactive. /// This function is called when the behaviour becomes disabled () or inactive.
/// </summary> /// </summary>
void OnDisable() private void OnDisable()
{ {
StencilMaterial.Remove (_unmaskMaterial); StencilMaterial.Remove(_unmaskMaterial);
StencilMaterial.Remove (_revertUnmaskMaterial); StencilMaterial.Remove(_revertUnmaskMaterial);
_unmaskMaterial = null; _unmaskMaterial = null;
_revertUnmaskMaterial = null; _revertUnmaskMaterial = null;
if (graphic) if (graphic)
{ {
var canvasRenderer = graphic.canvasRenderer; var canvasRenderer = graphic.canvasRenderer;
canvasRenderer.hasPopInstruction = false; canvasRenderer.hasPopInstruction = false;
canvasRenderer.popMaterialCount = 0; canvasRenderer.popMaterialCount = 0;
graphic.SetMaterialDirty(); graphic.SetMaterialDirty();
} }
SetDirty (); SetDirty();
} }
/// <summary> /// <summary>
/// LateUpdate is called every frame, if the Behaviour is enabled. /// LateUpdate is called every frame, if the Behaviour is enabled.
/// </summary> /// </summary>
void LateUpdate() private void LateUpdate()
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
if (m_FitTarget && (m_FitOnLateUpdate || !Application.isPlaying)) if (m_FitTarget && (m_FitOnLateUpdate || !Application.isPlaying))
#else #else
if (m_FitTarget && m_FitOnLateUpdate) if (m_FitTarget && m_FitOnLateUpdate)
#endif #endif
{ {
FitTo(m_FitTarget); FitTo(m_FitTarget);
} }
} }
#if UNITY_EDITOR #if UNITY_EDITOR
/// <summary> /// <summary>
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
/// </summary> /// </summary>
void OnValidate() private void OnValidate()
{ {
SetDirty(); SetDirty();
} }
#endif #endif
/// <summary> /// <summary>
/// Mark the graphic as dirty. /// Mark the graphic as dirty.
/// </summary> /// </summary>
void SetDirty() void SetDirty()
{ {
if (graphic) if (graphic)
{ {
graphic.SetMaterialDirty(); graphic.SetMaterialDirty();
} }
} }
} }
} }

View File

@ -1,46 +1,42 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Coffee.UIExtensions namespace Coffee.UIExtensions
{ {
/// <summary> /// <summary>
/// Unmask Raycast Filter. /// Unmask Raycast Filter.
/// The ray passes through the unmasked rectangle. /// The ray passes through the unmasked rectangle.
/// </summary> /// </summary>
[AddComponentMenu("UI/Unmask/UnmaskRaycastFilter", 2)] [AddComponentMenu("UI/Unmask/UnmaskRaycastFilter", 2)]
public class UnmaskRaycastFilter : MonoBehaviour, ICanvasRaycastFilter public class UnmaskRaycastFilter : MonoBehaviour, ICanvasRaycastFilter
{ {
//################################ //################################
// Serialize Members. // Serialize Members.
//################################ //################################
[Tooltip("Target unmask component. The ray passes through the unmasked rectangle.")] [Tooltip("Target unmask component. The ray passes through the unmasked rectangle.")]
[SerializeField] Unmask m_TargetUnmask; [SerializeField] private Unmask m_TargetUnmask;
//################################ //################################
// Public Members. // Public Members.
//################################ //################################
/// <summary> /// <summary>
/// Target unmask component. Ray through the unmasked rectangle. /// Target unmask component. Ray through the unmasked rectangle.
/// </summary> /// </summary>
public Unmask targetUnmask{ get { return m_TargetUnmask; } set { m_TargetUnmask = value; } } public Unmask targetUnmask { get { return m_TargetUnmask; } set { m_TargetUnmask = value; } }
/// <summary> /// <summary>
/// Given a point and a camera is the raycast valid. /// Given a point and a camera is the raycast valid.
/// </summary> /// </summary>
/// <returns>Valid.</returns> /// <returns>Valid.</returns>
/// <param name="sp">Screen position.</param> /// <param name="sp">Screen position.</param>
/// <param name="eventCamera">Raycast camera.</param> /// <param name="eventCamera">Raycast camera.</param>
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{ {
// Skip if deactived. // Skip if deactived.
if (!isActiveAndEnabled || !m_TargetUnmask || !m_TargetUnmask.isActiveAndEnabled) if (!isActiveAndEnabled || !m_TargetUnmask || !m_TargetUnmask.isActiveAndEnabled)
{ {
return true; return true;
} }
// check inside // check inside
if (eventCamera) if (eventCamera)
@ -51,18 +47,18 @@ namespace Coffee.UIExtensions
{ {
return !RectTransformUtility.RectangleContainsScreenPoint((m_TargetUnmask.transform as RectTransform), sp); return !RectTransformUtility.RectangleContainsScreenPoint((m_TargetUnmask.transform as RectTransform), sp);
} }
} }
//################################ //################################
// Private Members. // Private Members.
//################################ //################################
/// <summary> /// <summary>
/// This function is called when the object becomes enabled and active. /// This function is called when the object becomes enabled and active.
/// </summary> /// </summary>
void OnEnable() void OnEnable()
{ {
} }
} }
} }