自定义鼠标光标

你可能希望更改游戏中鼠标光标的外观,以便适应总体设计。自定义鼠标光标有两种方法:

  1. 使用项目设置

  2. 使用脚本

使用项目设置来自定义鼠标光标更简单(但也更受限)。第二种方法更方便定制,但涉及到脚本:

备注

You could display a “software” mouse cursor by hiding the mouse cursor and moving a Sprite2D to the cursor position in a _process() method, but this will add at least one frame of latency compared to an “hardware” mouse cursor. Therefore, it’s recommended to use the approach described here whenever possible.

如果你必须使用“软件”的做法,可以考虑添加一个外推步骤,以便更好地显示实际的鼠标输入。

使用项目设置

Open project settings, go to Display>Mouse Cursor. You will see Custom Image, Custom Image Hotspot and Tooltip Position Offset.

../../_images/cursor_project_settings.webp

自定义图像是希望设置为鼠标光标的图像. 自定义热点是图像中的点, 你希望将其用作光标的检测点.

警告

The custom image must be 256×256 pixels at most. To avoid rendering issues, sizes lower than or equal to 128×128 are recommended.

On the web platform, the maximum allowed cursor image size is 128×128.

使用脚本

创建一个 Node 节点并附加下面的脚本。

GDScriptC#

  1. extends Node
  2. # Load the custom images for the mouse cursor.
  3. var arrow = load("res://arrow.png")
  4. var beam = load("res://beam.png")
  5. func _ready():
  6. # Changes only the arrow shape of the cursor.
  7. # This is similar to changing it in the project settings.
  8. Input.set_custom_mouse_cursor(arrow)
  9. # Changes a specific shape of the cursor (here, the I-beam shape).
  10. Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
  1. public override void _Ready()
  2. {
  3. // Load the custom images for the mouse cursor.
  4. var arrow = ResourceLoader.Load("res://arrow.png");
  5. var beam = ResourceLoader.Load("res://beam.png");
  6. // Changes only the arrow shape of the cursor.
  7. // This is similar to changing it in the project settings.
  8. Input.SetCustomMouseCursor(arrow);
  9. // Changes a specific shape of the cursor (here, the I-beam shape).
  10. Input.SetCustomMouseCursor(beam, Input.CursorShape.Ibeam);
  11. }

参见

Check Input.set_custom_mouse_cursor()‘s documentation for more information on usage and platform-specific caveats.

光标列表

正如 Input 类中所述(请参阅 CursorShape 枚举),你可以定义多个鼠标光标。你想要使用哪一个取决于你的用例。

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.