【问题标题】:Click and Drag a gameobject in a Overlay Canvas in Unity在 Unity 的叠加画布中单击并拖动游戏对象
【发布时间】:2016-05-17 04:27:51
【问题描述】:

我有一个 Canvas 设置,它位于屏幕空间 - 叠加层中,我几乎找到了我想要的东西,但结果有点偏离。稍微偏离我的意思是当我单击并按住以从我的库存中拖动某些东西时,它并不完全是鼠标所在的位置,但它很接近。现在拖动不起作用,但是当我完成拖动时,我的游戏对象会正确返回到各自的位置。

我的代码:

void Awake(){
    rectTrans = GetComponent<RectTransform> ();
}

void Start(){
    localRectTrans = rectTrans.localPosition;
}

public void OnPointerUp(PointerEventData data){
    // IF we are dragging an item
    if(itemBeingDragged != null){
        itemBeingDragged = null;
        // return this gameobject to its original location.
        rectTrans.localPosition = localRectTrans;
    }
}

public void OnBeginDrag(PointerEventData data){
    // IF we have an item to drag.
    if(isItem){
        // Set the itemBeingDragged to this gameobject.
        itemBeingDragged = gameObject;
        // Get the starting mouse location for dragging.
        startMousePos = Input.mousePosition;
    }
}

public void OnDrag(PointerEventData data){
    // IF we have an item to drag in this inventory slot.
    if(isItem){
        // Get the location of the mouse.
        Vector2 curMousePosition = Input.mousePosition;
        // Get the difference in position from when the mouse was first clicked to where it is currently being dragged.
        Vector2 diffInPos = curMousePosition - startMousePos;
        // The current position based on the difference in position with the mouse and the local position of this inventory slot.
        Vector2 curPos = localRectTrans + diffInPos;
        transform.localPosition = curPos;
    }
}

我怎样才能让它实际上正好是我的鼠标光标所在的位置?我觉得我很接近但只是保持空白。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    使用RectTransformUtility.ScreenPointToLocalPointInRectangle

    public Canvas parentCanvasOfImageToMove;
    public Image imageToMove;   
    
    public void OnDrag(PointerEventData data)
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvasOfImageToMove.transform as RectTransform, data.position, parentCanvasOfImageToMove.worldCamera, out pos);
        imageToMove.transform.position = parentCanvasOfImageToMove.transform.TransformPoint(pos);
    }
    

    public Canvas parentCanvasOfImageToMove;
    public Image imageToMove; 
    
    public void Update()
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvasOfImageToMove.transform as RectTransform, Input.mousePosition, parentCanvasOfImageToMove.worldCamera, out pos);
        imageToMove.transform.position = parentCanvasOfImageToMove.transform.TransformPoint(pos);
    }
    

    要平滑运动,只需替换

    imageToMove.transform.position = parentCanvasOfImageToMove.transform.TransformPoint(pos);
    

    imageToMove.transform.position = Vector3.Lerp(imageToMove.transform.position, parentCanvasOfImageToMove.transform.TransformPoint(pos), Time.deltaTime * 30f);
    

    【讨论】:

    • 嘿程序员,感谢您的帮助,但我仍然收到错误消息。我使用上面的代码,我有一个 GIF 可以在这里查看:imgur.com/ED6YKU8。我想我需要进一步澄清并说 rectTrans 是我的库存槽,这是我当前的层次结构:clip2net.com/s/3y4J6bo。我还尝试在您链接到我的画布的 RectTransform 的代码中使用第一个参数,这就是它在 GIF 中的样子:imgur.com/2EeoTlf。如果您需要更多信息,请告诉我。
    • @JoeyL 你遗漏了一些信息,所以我做了一些猜测工作。请像这样i.imgur.com/2AGytrc.pngInventory Slot 1 的矩形变换对齐到左下角。测试一下。如果它不起作用,请撤消它,然后将 Inventory Bag 的 rect 变换对齐到左下角。如果它不起作用,请再次撤消它,然后将 Inventory 的矩形变换对齐到左下角。如果失败,请反馈。我有另一个解决方案。
    • @JoeyL 如果我上面所说的失败,请尝试yourInventory.localPosition = pos; 而不是rectTrans.anchoredPosition = pos; pos 是我答案中代码的位置。让我知道这些是否适合您。
    • 会的,现在试试。
    • 如果画布是屏幕空间覆盖,相机参数应该为空,否则你会得到不正确的结果,就像我在这个评论上面所说的那样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多