有没有一种方法可以在统一3d中拖放UI按钮?

时间:2020-10-17 05:12:15

标签: c# unity3d

是否可以在统一3d中拖放UI按钮?

我可以向按钮添加两个功能吗? 第一次单击通常会执行某些操作。 单击并按住时,可以将其拖放到屏幕上的其他位置吗?

这是我到目前为止的代码,但是无法在按钮上使用。

    void Update()
    {
        if (isDragged)
        {
            transform.position =  Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
    }

    public void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isDragged = true;
        }

        
    }
    public void OnMouseUp()
    {
        isDragged = false;
    }

1 个答案:

答案 0 :(得分:0)

您可以使用统一的内置界面,

使用IPointerClickHandler可以在单击时执行某些操作,并实现另外三个用于处理拖动的界面,分别为IBeginDragHandlerIDragHandlerIEndDragHandler

public class Example : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    //Detect if a click occurs
    public void OnPointerClick(PointerEventData pointerEventData)
    {
        Debug.Log(name + " Game Object Clicked!");
    }

    public void OnBeginDrag(PointerEventData eventData)
    {

    }

    public void OnDrag(PointerEventData data)
    {

    }

    public void OnEndDrag(PointerEventData eventData)
    {

    }
}

参考文献:https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IDragHandler.html