if (Input.touchSupported) {
if (Input.touchCount > 0) {
if(Input.touches[0].phase== TouchPhase.Began){
if (EventSystem.current.IsPointerOverGameObject (Input.GetTouch (0).fingerId))
isOnGUGI = true;
}
} else {
isOnGUGI = false;
}
} else {
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.IsPointerOverGameObject ())
isOnGUGI = true;
} else if (Input.GetMouseButtonUp (0)) {
isOnGUGI = false;
}
}
简单的处理方式,需要在Resources文件夹下面先放一个EventSystem的Prefab
/// <summary>
/// 判断鼠标是否在UI上面
/// </summary>
/// <returns><c>true</c>, if mouse on U was checked, <c>false</c> otherwise.</returns>
public static bool CheckMouseOnUI(){
if(EventSystem.current){
if(Input.touchSupported && Input.touchCount>0){
return EventSystem.current.IsPointerOverGameObject (Input.GetTouch(0).fingerId);
}else{
return EventSystem.current.IsPointerOverGameObject ();
}
}else{
GameObject.Instantiate(Resources.Load("UI/EventSystem"));
}
return false;
}