在制作UI时经常会遇到,点击别处时当前UI消失,每次要写一边很麻烦。
于是,自己写了一个单例来实现关于单个或多个面板点击别处消失问题。
代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public static class RaycastUIHelper
{
/// <summary>
/// 鼠标可选状态
/// </summary>
public enum MouseStatus
{
Down = 0,//左右键按下去
Up = 1,//左右键抬起
DownAndWheel = 2,//左右键按下去+滑轮
UpAndWheel = 3,//左右键抬起+滑轮
}
#region Public Methods
/// <summary>
/// 返回单个个UI检查值
/// </summary>
/// <param name="uiPanel"></param>
/// <param name="mouseStatus"></param>
/// <returns></returns>
public static bool CheckRaycastUI(Transform uiPanel, MouseStatus mouseStatus = MouseStatus.DownAndWheel)
{
switch (mouseStatus)
{
case MouseStatus.DownAndWheel:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanel);
}
return false;
case MouseStatus.Down:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
return GetCheckValue(uiPanel);
}
return false;
case MouseStatus.Up:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
return GetCheckValue(uiPanel);
}
return false;
case MouseStatus.UpAndWheel:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanel);
}
return false;
}
return false;
}
/// <summary>
/// 返回多个UI检查值
/// </summary>
/// <param name="uiPanels"></param>
/// <param name="mouseStatus"></param>
/// <returns></returns>
public static bool CheckRaycastUI(List<Transform> uiPanels, MouseStatus mouseStatus = MouseStatus.DownAndWheel)
{
switch (mouseStatus)
{
case MouseStatus.DownAndWheel:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.Down:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.Up:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.UpAndWheel:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanels);
}
return false;
}
return false;
}
/// <summary>
/// 返回多个UI检查值
/// </summary>
/// <param name="uiPanels"></param>
/// <param name="mouseStatus"></param>
/// <returns></returns>
public static bool CheckRaycastUI(Transform uiPanels1, Transform uiPanels2, MouseStatus mouseStatus = MouseStatus.DownAndWheel)
{
List<Transform> uiPanels = new List<Transform>(2) {
uiPanels1,
uiPanels2,
};
switch (mouseStatus)
{
case MouseStatus.DownAndWheel:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.Down:
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.Up:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
return GetCheckValue(uiPanels);
}
return false;
case MouseStatus.UpAndWheel:
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
return GetCheckValue(uiPanels);
}
return false;
}
return false;
}
/// <summary>
/// 检查鼠标是否在UI上
/// </summary>
/// <param name="uiPanel"></param>
/// <returns></returns>
public static bool CheckMouseOnUI()
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> result = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerEventData, result);
if (result.Count > 0)
{
return true;
}
return false;
}
return false;
}
/// <summary>
/// 多个个 检查值
/// </summary>
/// <param name="uiPanel"></param>
/// <returns></returns>
public static bool GetCheckValue(List<Transform> uiPanels)
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> result = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerEventData, result);
if (result.Count > 0)
foreach (var item in result)
{
if (uiPanels != null)
foreach (var u in uiPanels)
{
if (item.gameObject.transform.IsChildOf(u.transform))
{
return false;
}
}
}
}
return true;
}
#endregion
#region Private Methods
/// <summary>
/// 单个 检查值
/// </summary>
/// <param name="uiPanel"></param>
/// <returns></returns>
private static bool GetCheckValue(Transform uiPanel)
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> result = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerEventData, result);
if (result.Count > 0)
foreach (var item in result)
{
if (uiPanel != null)
if (item.gameObject.transform.IsChildOf(uiPanel.transform))
{
return false;
}
}
}
return true;
}
#endregion
}
这个拿上去就可以用了,