【问题标题】:Unity: Onmouseup while dragging UI element over it?Unity:在拖动 UI 元素时 Onmouseup?
【发布时间】: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 布尔值,因为它可能会触发它。

标签: c# unity3d


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但情况已经如此。两者都适用于physics2d 和 3d。顺便说一句,这是一个 2D 项目。
  • @RicardoFrederiks,好的。您为“非 UI”对象使用了什么类型的对撞机?那个对撞机设置正确吗?非 UI 对象使用什么旋转值?是 (0,0,0) 还是别的什么?
  • 嘿,很抱歉回复晚了 --> Gamescom。我只是使用了一个 2D 盒子对撞机,在启用和不启用“触发”的情况下进行了尝试。对撞机也够大。
  • @RicardoFrederiks,你能制作一些选中“非 UI”和“UI”对象的 Inspector 窗口截图吗?如果您可以发布一个显示问题的测试项目,那就太好了。
猜你喜欢
  • 2022-11-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多