在Unity3d中,如何检测UI上的触摸?
在制作Unity3d移动应用程序中。我有一个问题:如何在UI上检测触摸 我尝试过这个(但现在成功了) 还有这个在Unity3d中,如何检测UI上的触摸?,unity3d,touch,Unity3d,Touch,在制作Unity3d移动应用程序中。我有一个问题:如何在UI上检测触摸 我尝试过这个(但现在成功了) 还有这个 private static List<RaycastResult> tempRaycastResults = new List<RaycastResult>(); public bool PointIsOverUI(float x, float y) { var eventDataCurrentPosition = new PointerEventD
private static List<RaycastResult> tempRaycastResults = new List<RaycastResult>();
public bool PointIsOverUI(float x, float y)
{
var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(x, y);
tempRaycastResults.Clear();
EventSystem.current.RaycastAll(eventDataCurrentPosition, tempRaycastResults);
return tempRaycastResults.Count > 0;
}
private static List tempRaycastResults=new List();
公共bool PointIsOverUI(浮点x,浮点y)
{
var eventDataCurrentPosition=新点eventdata(EventSystem.current);
eventDataCurrentPosition.position=新矢量2(x,y);
tempRaycastResults.Clear();
EventSystem.current.RaycastAll(eventDataCurrentPosition,tempRaycastResults);
返回tempRaycastResults.Count>0;
}
对于移动设备,您需要将触摸id传递给iPointerOverGameObject
foreach (Touch touch in Input.touches)
{
int id = touch.fingerId;
if (EventSystem.current.IsPointerOverGameObject(id))
{
// ui touched
}
}
请试试这个:
// for Android check differently :
if(EventSystem.current.IsPointerOverGameObject(0) return false;
// for windows check as usual :
if (EventSystem.current.IsPointerOverGameObject())
return false;
将其添加到可单击对象
private MyUIHoverListener uiListener;
private Vector3 mousePos;
private void OnMouseDown()
{
mousePos = Input.mousePosition;
Debug.Log(Input.mousePosition);
}
private void OnMouseUp()
{
//this part helps to not trigger object when dragging
mousePos -= Input.mousePosition;
//Debug.Log(mousePos);
if(mousePos.x < 3 && mousePos.y < 3 && mousePos.z < 3 && mousePos.x > -3 &&
mousePos.y > -3 && mousePos.z > -3)
{
//Debug.Log("NOT MOVED");
if (!GameMan.ObjectClickBlocker)
{
if (uiListener.IsUIOverride)
{
// Debug.Log("Cancelled OnMouseDown! A UI element has override this object!");
}
else
{
// Debug.Log("Object OnMouseDown");
StartCoroutine(LoadThisBuilding( 0));
ToggleBuildingMenu();
}
}
}
}
可能的复制品我最近与此发生了冲突。奇怪的是,在我的项目中,在Unity 5.3.x
EventSystem.current.IsPointerOverGameObject()
在iOS(和Android!)中运行良好,但至少在Unity 5.4.0f3中,我必须为iOS添加一个id(在我的例子中,0
很好),但不适用于Android。如果使用IsPointerOverGameObject(),而不使用参数,它会指向“鼠标左键”(pEnIDID=-1);因此,当使用ISPOTION ObjgObjor对象进行触摸时,应该考虑将PoTrID传递给它。
private MyUIHoverListener uiListener;
private Vector3 mousePos;
private void OnMouseDown()
{
mousePos = Input.mousePosition;
Debug.Log(Input.mousePosition);
}
private void OnMouseUp()
{
//this part helps to not trigger object when dragging
mousePos -= Input.mousePosition;
//Debug.Log(mousePos);
if(mousePos.x < 3 && mousePos.y < 3 && mousePos.z < 3 && mousePos.x > -3 &&
mousePos.y > -3 && mousePos.z > -3)
{
//Debug.Log("NOT MOVED");
if (!GameMan.ObjectClickBlocker)
{
if (uiListener.IsUIOverride)
{
// Debug.Log("Cancelled OnMouseDown! A UI element has override this object!");
}
else
{
// Debug.Log("Object OnMouseDown");
StartCoroutine(LoadThisBuilding( 0));
ToggleBuildingMenu();
}
}
}
}
public class MyUIHoverListener : MonoBehaviour
{
[SerializeField]
public bool IsUIOverride { get; private set; }
void Update()
{
// It will turn true if hovering any UI Elements
IsUIOverride = EventSystem.current.IsPointerOverGameObject();
}
void OnDisable()
{
IsUIOverride = false;}
}