简体   繁体   English

在Unity中的“叠加画布”中单击并拖动游戏对象

[英]Click and Drag a gameobject in a Overlay Canvas in Unity

I have a Canvas setup and it is in Screen Space - Overlay and I almost have what I am looking for but the results are just a little bit off. 我有一个Canvas设置,它在“屏幕空间-重叠”中,我几乎可以找到所需的内容,但结果略有偏离。 By a little bit off I mean when I click and hold to drag something from my inventory it isn't exactly where the mouse is but it is close. 稍微偏离一下,是指当我单击并按住以从清单中拖出某些东西时,它并不完全位于鼠标的位置,而是接近的。 Now the dragging doesn't work but when I am done dragging my GameObject returns to its respective place correctly. 现在拖动不起作用,但是完成拖动后,我的GameObject会正确返回其相应位置。

My Code : 我的代码:

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;
    }
}

How can I get this to actually be exactly where my mouse cursor is? 我怎样才能使它实际上恰好是我的鼠标光标所在的位置? I feel I am so close but just keep drawing a blank. 我觉得我已经很近了,但是请保持空白。

Use RectTransformUtility.ScreenPointToLocalPointInRectangle . 使用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);
}

OR 要么

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);
}

To Smooth the movement simply replace 要使运动平稳,只需更换

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM