我怎样才能使用鼠标滚轮 select 每次另一个 Tool/s?

How can i use the mouse wheel to select each time another Tool/s?

private void OnEnable()
{
    Tools.current = Tool.View;
    Tools.viewTool = ViewTool.FPS;
}

该工具具有一些属性,例如矩形、缩放、旋转... 我想在工具属性之间使用鼠标滚轮向上向下 select。

如果您使用的是 unity 应用程序,可以试试这个:

 var d = Input.GetAxis("Mouse ScrollWheel");
 if (d > 0f)
 {
     // scroll up
 }
 else if (d < 0f)
 {
     // scroll down
 }

source

并不复杂。

1。使用 Input.GetAxisRaw instead Input.GetAxis because using Input.GetAxis 读取鼠标滚动输入会随着时间的推移平滑滚动值,这不是您需要的。

2.检查滚动值是否大于某个阈值。如果是,则鼠标滚轮正在向上滚动。如果它小于那个值,那么它正在向下滚动。

3。根据结果 #2,创建一个名为 "level" 的全局整数,您可以根据滚轮的方向。

4Tool 枚举值从 -1 开始到 4 因为它是这样声明的:

public enum Tool
{
    None = -1,
    View = 0,
    Move = 1,
    Rotate = 2,
    Scale = 3,
    Rect = 4
}

#3 中的 "level" 变量限制为 -14 之间的值 Mathf.Clamp(level, -1, 4);.

5。最后,将 int "level" 变量转换为 Tool 枚举并将结果赋值给 Tools.current.

使用 int 转换为 Tool enum 防止使用许多 if 或 switch 语句。

float wheelThreshold = 0.09f;
int level = -1;

void Update()
{
    level = (int)Tools.current;

    float mouseScrollDir = Input.GetAxisRaw("Mouse ScrollWheel");

    if (mouseScrollDir > wheelThreshold)
    {
        Debug.Log("Scroll Up");
        level++;
        level = Mathf.Clamp(level, -1, 4);
        Tools.current = (Tool)level;
    }
    else if (mouseScrollDir < -wheelThreshold)
    {
        Debug.Log("Scroll Down");
        level--;
        level = Mathf.Clamp(level, -1, 4);
        Tools.current = (Tool)level;
    }
}

结果: