unity 获取鼠标滚轮信息(UGUI)

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

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

public virtual void OnScroll(PointerEventData eventData)
{
     int num;
        if (eventData.scrollDelta.y == 1)
        {
            //+
            num = 1;
        }
        else
        {
            //-
            num = -1;
        }
}

该函数接收的参数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
        }
    }
}

Input获取滚轮信息

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

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

猜你喜欢

转载自blog.csdn.net/qq_37524903/article/details/131051526