Unity获取鼠标滚轮信息

Unity获取鼠标滚轮信息

UGUI获取滚轮信息

UGUI关于鼠标滚轮信息的获取有一个专门的接口IScrollHandler用于接收滚轮事件。继承该事件之后便需要实现函数OnScroll
如下:

public virtual void OnScroll(PointerEventData eventData)
{
    
}

该函数接收的参数PointerEventData,如下:

using System;
using System.Collections.Generic;

namespace UnityEngine.EventSystems
{
    //
    // 摘要:
    //     Event payload associated with pointer (mouse / touch) events.
    public class PointerEventData : BaseEventData
    {
        //
        // 摘要:
        //     List of objects in the hover stack.
        public List<GameObject> hovered;

        public PointerEventData(EventSystem eventSystem);

        //
        // 摘要:
        //     The GameObject that received the OnPointerDown.
        public GameObject pointerPress { get; set; }
        //
        // 摘要:
        //     The camera associated with the last OnPointerPress event.
        public Camera pressEventCamera { get; }
        //
        // 摘要:
        //     The camera associated with the last OnPointerEnter event.
        public Camera enterEventCamera { get; }
        //
        // 摘要:
        //     The EventSystems.PointerEventData.InputButton for this event.
        public InputButton button { get; set; }
        //
        // 摘要:
        //     Determines whether the user is dragging the mouse or trackpad.
        public bool dragging { get; set; }
        //
        // 摘要:
        //     Should a drag threshold be used?
        public bool useDragThreshold { get; set; }
        //
        // 摘要:
        //     The amount of scroll since the last update.
        public Vector2 scrollDelta { get; set; }
        //
        // 摘要:
        //     Number of clicks in a row.
        public int clickCount { get; set; }
        //
        // 摘要:
        //     The last time a click event was sent.
        public float clickTime { get; set; }
        [Obsolete("Use either pointerCurrentRaycast.worldNormal or pointerPressRaycast.worldNormal")]
        public Vector3 worldNormal { get; set; }
        [Obsolete("Use either pointerCurrentRaycast.worldPosition or pointerPressRaycast.worldPosition")]
        public Vector3 worldPosition { get; set; }
        //
        // 摘要:
        //     The screen space coordinates of the last pointer click.
        public Vector2 pressPosition { get; set; }
        //
        // 摘要:
        //     Pointer delta since last update.
        public Vector2 delta { get; set; }
        //
        // 摘要:
        //     Current pointer position.
        public Vector2 position { get; set; }
        //
        // 摘要:
        //     Identification of the pointer.
        public int pointerId { get; set; }
        public bool eligibleForClick { get; set; }
        //
        // 摘要:
        //     RaycastResult associated with the pointer press.
        public RaycastResult pointerPressRaycast { get; set; }
        //
        // 摘要:
        //     RaycastResult associated with the current event.
        public RaycastResult pointerCurrentRaycast { get; set; }
        //
        // 摘要:
        //     The object that is receiving OnDrag.
        public GameObject pointerDrag { get; set; }
        //
        // 摘要:
        //     The object that the press happened on even if it can not handle the press event.
        public GameObject rawPointerPress { get; set; }
        //
        // 摘要:
        //     The GameObject for the last press event.
        public GameObject lastPress { get; }
        //
        // 摘要:
        //     The object that received 'OnPointerEnter'.
        public GameObject pointerEnter { get; set; }

        //
        // 摘要:
        //     Is the pointer moving.
        //
        // 返回结果:
        //     Moving.
        public bool IsPointerMoving();
        //
        // 摘要:
        //     Is scroll being used on the input device.
        //
        // 返回结果:
        //     Scrolling.
        public bool IsScrolling();
        public override string ToString();

        //
        // 摘要:
        //     Input press tracking.
        public enum InputButton
        {
            //
            // 摘要:
            //     Left button.
            Left = 0,
            //
            // 摘要:
            //     Right button.
            Right = 1,
            //
            // 摘要:
            //     Middle button.
            Middle = 2
        }
        //
        // 摘要:
        //     The state of a press for the given frame.
        public enum FramePressState
        {
            //
            // 摘要:
            //     Button was pressed this frame.
            Pressed = 0,
            //
            // 摘要:
            //     Button was released this frame.
            Released = 1,
            //
            // 摘要:
            //     Button was pressed and released this frame.
            PressedAndReleased = 2,
            //
            // 摘要:
            //     Same as last frame.
            NotChanged = 3
        }
    }
}

其中,Vector2类型的字段scrollDelta中的y,便是滚轮滚动的信息具体数值信息:上滑滚轮为正1,下滑滚轮为负1

Input获取滚轮信息

通过Input获取滚轮信息的方式为Input.GetAxis("Mouse ScrollWheel")

public void OnUpdate()
{
    Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
}

通过测试便可得到如下效果:
结果
滚轮上滑为正,下滑为负;且滚轮的每个小格卡顿,其数值信息表示为0.1,快速连续滚动时其数值会直接出现对应的数值,不会一格一格出现。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天富儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值