判断是鼠标拖拽还是点击

using UnityEngine;
using System.Collections;
using Deduction;
public class MouseInput : MonoBehaviour {

    public static MouseInput instance;

    void Awake()
    {
        instance = this;
    }

    //鼠标左键事件
    public event mousePosHandler leftDownEvent;
    public event mousePosHandler leftUpEvent;
    public event mousePosHandler leftDragEvent;
    public event mousePosHandler leftDragDeltaEvent;
    public event mousePosHandler leftRelaxedEvent;
    public event mousePosHandler leftClickEvent;

    Vector2 posLeftDown;//记录鼠标左键按下的位置
    Vector2 posLeftPre;
    bool isLeftPressed = false;
    bool clickEnable = true;

    //鼠标滚轮事件
    public event mouseScrollHandler mouseScrollEvent;

    //鼠标中键事件
    public event mousePosHandler middleDownEvent;
    public event mousePosHandler middleUpEvent;
    public event mousePosHandler middleDragEvent;
    public event mousePosHandler middleDragDeltaEvent;
    public event mousePosHandler middleRelaxedEvent;

    Vector2 posMiddlePre;
    bool isMiddlePressed = false;
    
    void Update()
    {
        //鼠标左键
        if (Input.GetMouseButtonDown(0))
        {
            isLeftPressed=true;
            clickEnable = true;
            posLeftDown = Input.mousePosition;
            posLeftPre = Input.mousePosition;
            if(leftDownEvent!=null)
                leftDownEvent(Input.mousePosition);
        }

        if (isLeftPressed)
        {
            if(((Vector2)Input.mousePosition-posLeftDown).sqrMagnitude>9)
            {
                clickEnable = false;
            }
            if (!clickEnable)
            {
                if (leftDragEvent != null)
                    leftDragEvent(Input.mousePosition);
                Vector2 leftDragDelta = (Vector2)Input.mousePosition -posLeftPre;
                posLeftPre = Input.mousePosition;
                if (leftDragDeltaEvent != null)
                    leftDragDeltaEvent(leftDragDelta);
            }
        }
        else
        {
            if (leftRelaxedEvent!=null)
            {
                leftRelaxedEvent(Input.mousePosition);
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            isLeftPressed = false;
            if (leftUpEvent != null)
                leftUpEvent(Input.mousePosition);
            if(clickEnable)
            {
                if (leftClickEvent != null)
                    leftClickEvent(Input.mousePosition);
            }
        }

        //鼠标中键
        if (Input.GetMouseButtonDown(2))
        {
            isMiddlePressed = true;
            posMiddlePre = Input.mousePosition;
            if (middleDownEvent != null)
                middleDownEvent(Input.mousePosition);
        }

        if (isMiddlePressed)
        {
            if (middleDragEvent != null)
                middleDragEvent(Input.mousePosition);
            Vector2  middleDragDelta = (Vector2)Input.mousePosition - posMiddlePre;
                posMiddlePre = Input.mousePosition;
                if (middleDragDeltaEvent != null)
                    middleDragDeltaEvent(middleDragDelta);    
        }
        else
        {
            if (middleRelaxedEvent != null)
                middleRelaxedEvent(Input.mousePosition);
        }

        if (Input.GetMouseButtonUp(2))
        {
            isMiddlePressed = false;
            if (middleUpEvent != null)
                middleUpEvent(Input.mousePosition);
        }

        //鼠标滚轮
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (!Mathf.Approximately(scroll,0))
        {
            if (mouseScrollEvent != null)
                mouseScrollEvent(scroll);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值