UnmaskForUGUI/Scripts/UnmaskRaycastFilter.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2022-02-18 02:48:23 +08:00
using UnityEngine;
2018-10-14 15:51:54 +08:00
namespace Coffee.UIExtensions
{
2022-02-18 02:48:23 +08:00
/// <summary>
/// Unmask Raycast Filter.
/// The ray passes through the unmasked rectangle.
/// </summary>
[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;
2018-10-14 15:51:54 +08:00
2022-02-18 02:48:23 +08:00
//################################
// Public Members.
//################################
/// <summary>
/// Target unmask component. Ray through the unmasked rectangle.
/// </summary>
public Unmask targetUnmask { get { return m_TargetUnmask; } set { m_TargetUnmask = value; } }
2018-10-14 15:51:54 +08:00
2022-02-18 02:48:23 +08:00
/// <summary>
/// Given a point and a camera is the raycast valid.
/// </summary>
/// <returns>Valid.</returns>
/// <param name="sp">Screen position.</param>
/// <param name="eventCamera">Raycast camera.</param>
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
// Skip if deactived.
if (!isActiveAndEnabled || !m_TargetUnmask || !m_TargetUnmask.isActiveAndEnabled)
{
return true;
}
2018-10-14 15:51:54 +08:00
// check inside
if (eventCamera)
{
return !RectTransformUtility.RectangleContainsScreenPoint((m_TargetUnmask.transform as RectTransform), sp, eventCamera);
}
else
{
return !RectTransformUtility.RectangleContainsScreenPoint((m_TargetUnmask.transform as RectTransform), sp);
}
2022-02-18 02:48:23 +08:00
}
2018-10-14 15:51:54 +08:00
2022-02-18 02:48:23 +08:00
//################################
// Private Members.
//################################
2018-10-14 15:51:54 +08:00
2022-02-18 02:48:23 +08:00
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
void OnEnable()
{
}
}
2018-10-14 15:51:54 +08:00
}