unity2d防止UI按钮被点击时raycast2d打
问题描述:
我有它被设计为可点击collider2d一个游戏物体,并且我也有一个UI按钮将跟随而玩家移动相机,事情是,他们有时可能会重叠,并且当它们重叠时,当用户点击重叠区域时,我确定用户想要点击对象(这是通过raycast2d命中的),所以我应该防止按钮被点击。unity2d防止UI按钮被点击时raycast2d打
可点击游戏物体的光线投射植入该脚本如下:
private void checkTouch()
{
if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
{
Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);
if (hit)
{
Debug.Log(hit.collider.gameObject + "is hit");
IInputBehavior inputBehavior = hit.collider.gameObject.GetComponent<IInputBehavior>();
//IInputVehavior was a self-desgined C# interface which has a `OnClick()` method.
if (inputBehavior != null)
{
//here we should prevent the UIButton to be clicked, but how?
inputBehavior.OnClick();
}
}
}
}
答
好了,所以我在工作,所以我不能给你确切的代码,但我可以为您指出正确的方向。当你所做的画布本来它应该产生一个事件系统(如在场景中的游戏对象的地方),你需要在你想不点击,当用户界面是在它的游戏对象引用此。 在你的游戏中的对象是这样的:
if(GetComponent<EventSystem>().IsPointerOverGameObject) {
// The mouse is obscured by a UI element
} else {
// The mouse is not over a UI element
}
https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html
我的问题是如何防止UI按钮被点击,您引用的代码是如何检测,如果被点击的UI按钮,如果我知道你了吗? – armnotstrong