1,判断位置是否在ui上
void Update()
{if (Input.anyKeyDown)
{
bool isOnBtn = false;
isOnBtn = IsOverGUI(Input.mousePosition);
if (!isOnBtn)
{
}
}
}
private bool IsOverGUI(Vector2 pos)
{
EventSystem es = EventSystem.current;
PointerEventData ped = new PointerEventData(es);
ped.position = pos;
List<RaycastResult> rr = new List<RaycastResult>();
rr.Clear();
es.RaycastAll(ped, rr);
for (int i = 0; i < rr.Count; i++)
{
Debug.LogError("====="+ rr[i].gameObject.name);
}
return rr.Count > 0;//表示该位置在ui上
}
2,判断是否在某一ui上
private gameobject obj;
void Update()
{
if (Input.anyKeyDown)
{
bool isOnBtn = false;
isOnBtn = IsOverGUI(Input.mousePosition,obj);
if (!isOnBtn)
{
}
}
}
private bool IsOverGUI(Vector2 pos,GameObject target = null)
{
EventSystem es = EventSystem.current;
PointerEventData ped = new PointerEventData(es);
ped.position = pos;
List<RaycastResult> rr = new List<RaycastResult>();
rr.Clear();
es.RaycastAll(ped, rr);
for (int i = 0; i < rr.Count; i++)
{
if (target != null && rr[i].gameObject == target)
{
return true;//表示该位置pos在ui物体target上
}
}
return false;
}