Unity:在将UI元素拖到其上时向上鼠标?



在我的 2D 项目中,在将 UI 对象拖到上面时,我似乎无法让 OnMouseUp 工作。我希望能够检测到我是否将 UI 对象放在了非 UI 游戏对象上,以便我可以激活脚本,使 UI 对象的位置与每次更新的非 UI 对象的位置相同。

目前,我在UI对象上使用IOnDragHandler和IEndDragHandler进行拖动过程。当我想将 UI 对象放在另一个 UI 对象上时,IDropHandler 可以帮助我,但是当我想将其放在非 UI 游戏对象上时,我希望能够使用 OnMouseUp 事件,但它不会触发。

每当我开始拖动对象时,我都会在UI对象上使用CanvasGroup,并确保它不会被任何光线投射击中。

我还尝试将非 UI 对象的碰撞器设置为触发器,但它没有效果。此外,当我将UI元素拖到非UI元素上时,OnMouseOver和OnMouseExit确实有效。

非常感谢您对此进行调查。代码如下:

所以在UI对象(A卡(上,我使用这个:

public void OnDrag(PointerEventData eventData)
{
if (CanDrag)
{
if (!isDragging)
{
isDragging = true;
if (Slot != null)
{
Slot.GetComponent<FamilyTreePlacementScript>().CurrentFamilyCard = null;
}
if (card is FamilyCard || card is EquipmentCard) UIManagerScript.Instance.ToggleFamilyTree(null, true);
GetComponent<CanvasGroup>().blocksRaycasts = false;
CardManagerScript.CardBeingDragged = gameObject;
DataKeeperScript.Instance.MayDragCamera = false;
CardManagerScript.Instance.DeletePreview();
transform.SetParent(canvas);
}
transform.position = Input.mousePosition;
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (CanDrag)
{
isDragging = false;
GetComponent<CanvasGroup>().blocksRaycasts = true;
CardManagerScript.CardBeingDragged = null;
DataKeeperScript.Instance.MayDragCamera = true;
if (Slot == null)
{
transform.SetParent(defaultParent);
}
else
{
Slot.GetComponent<FamilyTreePlacementScript>().CurrentFamilyCard = card as FamilyCard;
transform.SetParent(Slot);
transform.position = Slot.position;
}
}        
}

在我想"放"卡的非 UI 对象上,我使用:

private void OnMouseUp()
{
if (CardManagerScript.CardBeingDragged)
{
Card card = CardManagerScript.CardBeingDragged.GetComponent<CardObjectScript>().card;
if (card is TakeOverCard)
{
//Check if the card can actually be played
if (Business.Allegiance != CardManagerScript.Instance.PlayerFamily)
{
CurrentTakeOverCard = card;
CardManagerScript.CardBeingDragged.GetComponent<CardObjectScript>().SetWorldObject(transform);
}
}
}
}

检查"查询命中触发器"(项目设置>物理(。它应该设置为"真">

相关内容

最新更新