在制作UI时经常会遇到,点击别处时当前UI消失,每次要写一边很麻烦。
于是,自己写了一个单例来实现关于单个或多个面板点击别处消失问题。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class RaycastUIHelper
{
private static RaycastUIHelper mInstance = null;
private static object mLock = new object();
public static RaycastUIHelper Instance
{
get
{
lock (mLock)
{
if (mInstance == null)
{
mInstance = new RaycastUIHelper();
}
}
return mInstance;
}
}
///
/// 鼠标可选状态
///
public enum MouseStatus
{
Down=0,//左右键按下去
Up=1,//左右键抬起
DownAndWheel =2,//左右键按下去+滑轮
UpAndWheel=3,//左右键抬起+滑轮
}
///
/// 返回单个个UI检查值
///
///
///
///
public 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;
}
///
/// 返回多个UI检查值
///
///
///
///
public bool CheckRaycastUI(List 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;
}
///
/// 返回多个UI检查值
///
///
///
///
public bool CheckRaycastUI(Transform uiPanels1, Transform uiPanels2, MouseStatus mouseStatus = MouseStatus.DownAndWheel)
{
List uiPanels = new List(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;
}
///
/// 检查鼠标是否在UI上
///
///
///
public bool CheckMouseOnUI()
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List result = new List();
EventSystem.current.RaycastAll(pointerEventData, result);
if (result.Count > 0)
{
return true;
}
return false;
}
return false;
}
///
/// 单个 检查值
///
///
///
private bool GetCheckValue(Transform uiPanel)
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List result = new List();
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;
}
///
/// 多个个 检查值
///
///
///
public bool GetCheckValue(List uiPanels)
{
if (EventSystem.current.IsPointerOverGameObject())
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List result = new List();
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;
}
}
这个拿上去就可以用了,