[英]Unity: Onmouseup while dragging UI element over it?
In my 2D project I can't seem to get OnMouseUp working while dragging a UI object over it. 在我的2D项目中,将UI对象拖到OnMouseUp上似乎无法正常工作。 I want to be able to detect that I dropped the UI object on a non-UI gameobject so I can activate the script that lets the position of the UI object be the same position as the non-UI object each Update.
我希望能够检测到我已将UI对象放置在非UI游戏对象上,因此我可以激活脚本,以使UI对象的位置与每个Update的非UI对象的位置相同。
Currenty I use the IOnDragHandler and IEndDragHandler on the UI object for the dragging process. 当前,我在UI对象上使用IOnDragHandler和IEndDragHandler进行拖动过程。 When I want to drop the UI object on another UI object then IDropHandler helps me, but when I want to drop it on a non-UI GameObject, I want to be able to use the OnMouseUp event, but it doesn't fire.
当我想将UI对象拖放到另一个UI对象上时,IDropHandler可以帮助我,但是当我要将其拖放到非UI GameObject上时,我希望能够使用OnMouseUp事件,但是不会触发。
Whenever I start dragging the object I use a CanvasGroup on the UI object and I make sure that it doesn't get hit by any raycasts. 每当我开始拖动对象时,我都会在UI对象上使用CanvasGroup,并确保它不会被任何射线投射击中。
I also tried making the collider of the non-UI object a trigger but it has no effect. 我还尝试使非UI对象的对撞机触发,但没有效果。 Also, OnMouseOver and OnMouseExit do work when I am dragging the UI element over the non-UI element.
另外,当我将UI元素拖到非UI元素上时,OnMouseOver和OnMouseExit确实起作用。
Thanks very much for looking into this. 非常感谢您对此进行调查。 Here comes the code:
代码如下:
So on the UI object (A card) I use this: 因此,在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;
}
}
}
And on the non-UI object that I want to "drop" the card on, I use: 在要“拖放”卡的非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);
}
}
}
}
Check the 'Queries Hit Triggers' (Project Settings > Physics). 检查“查询命中触发器”(“项目设置”>“物理”)。 It should be set to 'true'
应该设置为“ true”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.