当前位置:   article > 正文

Unity 获取鼠标滚轮信息的一些方法_unity 鼠标滚轮

unity 鼠标滚轮

Unity获取鼠标滚轮信息有以下一些方法:

1、使用Input.GetAxis(“Mouse ScrollWheel”)来获取鼠标滚轮的信息,如:

 //法一:

        float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");

        if (scrollWheelInput > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (scrollWheelInput < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、使用mouseScrollDelta变量,如:

 //法二:

        float scrollWheelInput1 = Input.mouseScrollDelta.y;

        if (scrollWheelInput1 > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (scrollWheelInput1 < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3、假如我们想只在UGUI上滚动鼠标滚轮才获取信息,可以使用一个专门的接口:IScrollHandler,实现的方法是:

    public void OnScroll(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }
  • 1
  • 2
  • 3
  • 4

该函数的参数PointerEventData具有很多相关的数据:

    public class PointerEventData : BaseEventData
    {
        public List<GameObject> hovered;

        public PointerEventData(EventSystem eventSystem);

        public bool useDragThreshold { get; set; }
        public bool dragging { get; set; }
        public InputButton button { get; set; }
        public float pressure { get; set; }
        public float tangentialPressure { get; set; }
        public float altitudeAngle { get; set; }
        public float azimuthAngle { get; set; }
        public float twist { get; set; }
        public Vector2 radius { get; set; }
        public Vector2 radiusVariance { get; set; }
        public bool fullyExited { get; set; }
        public bool reentered { get; set; }
        public Camera enterEventCamera { get; }
        public Camera pressEventCamera { get; }
        public GameObject pointerPress { get; set; }
        public Vector2 scrollDelta { get; set; }
        public int clickCount { get; set; }
        public float clickTime { get; set; }
        [Obsolete("Use either pointerCurrentRaycast.worldNormal or pointerPressRaycast.worldNormal")]
        public Vector3 worldNormal { get; set; }
        public GameObject pointerEnter { get; set; }
        public GameObject lastPress { get; }
        public GameObject rawPointerPress { get; set; }
        public GameObject pointerDrag { get; set; }
        public RaycastResult pointerCurrentRaycast { get; set; }
        public RaycastResult pointerPressRaycast { get; set; }
        public GameObject pointerClick { get; set; }
        public int pointerId { get; set; }
        public Vector2 position { get; set; }
        public Vector2 delta { get; set; }
        public Vector2 pressPosition { get; set; }
        [Obsolete("Use either pointerCurrentRaycast.worldPosition or pointerPressRaycast.worldPosition")]
        public Vector3 worldPosition { get; set; }
        public bool eligibleForClick { get; set; }

        public bool IsPointerMoving();
        public bool IsScrolling();
        public override string ToString();

        public enum InputButton
        {
            Left = 0,
            Right = 1,
            Middle = 2
        }
        public enum FramePressState
        {
            Pressed = 0,
            Released = 1,
            PressedAndReleased = 2,
            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

其中,scrollDelta属性与上面方法1/2的数据一样。所以我们可以这样实现方法以获取滚轮信息:

//法三:
    public void OnScroll(PointerEventData eventData)
    {
        if (eventData.scrollDelta.y > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (eventData.scrollDelta.y < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用法三,需要注意的是:只有我们把控制脚本放在相应的UGUI物体上才有效,或者放在Canvas下对所有UGUI有效。

但使用该方法的好处是:我们可以通过UGUI实现在指定获取滚轮信息。

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

闽ICP备14008679号