当前位置:   article > 正文

Unity获取鼠标滚轮信息_unity 鼠标滚轮

unity 鼠标滚轮

Unity获取鼠标滚轮信息

UGUI获取滚轮信息

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

public virtual void OnScroll(PointerEventData eventData)
{
    
}
  • 1
  • 2
  • 3
  • 4

该函数接收的参数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
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157

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

Input获取滚轮信息

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

public void OnUpdate()
{
    Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
}
  • 1
  • 2
  • 3
  • 4

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/127263
推荐阅读
相关标签
  

闽ICP备14008679号