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

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

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

复制代码
 //法一:

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

        if (scrollWheelInput > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (scrollWheelInput < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }

2、使用mouseScrollDelta变量,如:

复制代码
 //法二:

        float scrollWheelInput1 = Input.mouseScrollDelta.y;

        if (scrollWheelInput1 > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (scrollWheelInput1 < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }

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

复制代码
    public void OnScroll(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }

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

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

复制代码
//法三:
    public void OnScroll(PointerEventData eventData)
    {
        if (eventData.scrollDelta.y > 0f)
        {
            Debug.Log("向上滚动鼠标滚轮");
        }
        else if (eventData.scrollDelta.y < 0f)
        {
            Debug.Log("向下滚动鼠标滚轮");
        }
    }

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

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

相关推荐
死也不注释5 小时前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎
vfvfb11 小时前
定时点击二次鼠标 定时点击鼠标
计算机外设·自动点击·定时点击鼠标·倒计时点击鼠标·定时点击
小赖同学啊14 小时前
物联网中的Unity/Unreal引擎集成:数字孪生与可视化控制
物联网·unity·游戏引擎
Zlzxzw17 小时前
使用unity创建项目,进行动画制作
unity·游戏引擎
szekl20 小时前
HDMI 2.0 4×2矩阵切换器412HN——多信号输入输出的高清解决方案
linux·矩阵·计算机外设·电脑·ekl
X_StarX21 小时前
【Unity笔记01】基于单例模式的简单UI框架
笔记·ui·unity·单例模式·游戏引擎·游戏开发·大学生
九班长1 天前
Golang服务端处理Unity 3D游戏地图与碰撞的详细实现
3d·unity·golang
ysn111111 天前
NGUI实现反向定位到层级面板结点
unity
Thomas_YXQ1 天前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
KhalilRuan1 天前
Unity-MMORPG内容笔记-其一
unity·游戏引擎