Unity中判断点击按钮时鼠标事件(左键、中键、右键)

	public UnityEvent leftClick;
    public UnityEvent middleClick;
    public UnityEvent rightClick;


    private void Start()
    {
        leftClick.AddListener(new UnityAction(ButtonLeftClick));
        middleClick.AddListener(new UnityAction(ButtonMiddleClick));
        rightClick.AddListener(new UnityAction(ButtonRightClick));
    }


    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
            leftClick.Invoke();
        else if (eventData.button == PointerEventData.InputButton.Middle)
            middleClick.Invoke();
        else if (eventData.button == PointerEventData.InputButton.Right)
            rightClick.Invoke();
    }


    private void ButtonLeftClick()
    {
        Debug.Log("Button Left Click");
    }

    private void ButtonMiddleClick()
    {
        Debug.Log("Button Middle Click");
    }
    
    private void ButtonRightClick()
    {
        Debug.Log("Button Right Click");
    }

你可能感兴趣的:(Unity学习)