Version: 2021.3

Input.mouseScrollDelta

切换到手册
public static Vector2 mouseScrollDelta ;

描述

当前的鼠标滚动增量。(只读)

Input.mouseScrollDelta 存储在 Vector2.y 属性中。(Vector2.x 值将被忽略。)Input.mouseScrollDelta 可以是正数(向上)或负数(向下)。未旋转鼠标滚轮时,该值为零。注意,PC 上一般采用带有中心滚轮的鼠标。新版 macOS 在触控板上通过双指上下移动来模拟中心滚动。需要根据滚动速率,调整 mouseScrollDelta 返回的值。下面的示例中,使用了 0.1fscale

注意,mouseScrollDelta 为只读。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Input.mouseScrollDelta example // // Create a sphere moved by a mouse scrollwheel or two-finger // slide on a Mac trackpad.

public class ExampleClass : MonoBehaviour { private Transform sphere; private float scale;

void Awake() { GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere = go.transform;

// create a yellow quad go = GameObject.CreatePrimitive(PrimitiveType.Quad); go.transform.Rotate(new Vector3(90.0f, 0.0f, 0.0f)); go.transform.localScale = new Vector3(4.0f, 4.0f, 4.0f); go.GetComponent<Renderer>().material.color = new Color(0.75f, 0.75f, 0.0f, 0.5f);

// change the camera color and position Camera.main.clearFlags = CameraClearFlags.SolidColor; Camera.main.transform.position = new Vector3(2, 1, 5); Camera.main.transform.Rotate(0, -160, 0);

scale = 0.1f; }

void OnGUI() { Vector3 pos = sphere.position; pos.y += Input.mouseScrollDelta.y * scale; sphere.position = pos; } }