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,快速连续滚动时其数值会直接出现对应的数值,不会一格一格出现。