【发布时间】:2019-08-18 16:10:04
【问题描述】:
在我的 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);
}
}
}
}
【问题讨论】:
-
所以如果你在
MouseUp()的最顶部放置一个断点或Debug.Log,当你在你的游戏对象上释放它时它会停止/显示任何东西吗? -
嘿,很抱歉回复晚了 --> Gamescom。仅当我将鼠标悬停在非 UI 对象上而不拖动卡片时才会显示 debug.log。只要 UI 卡片对象位于鼠标和非 UI 对象之间,debug.log 就不会显示。
-
嗯,那是因为您需要禁用您正在拖动的 UI 元素上的
RaycastTarget布尔值,因为它可能会触发它。