Customizing the mouse cursor

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

  1. 使用项目设置
  2. 使用脚本

Using project settings is a simpler (but more limited) way to customize the mouse cursor. The second way is more customizable, but involves scripting.

注解

You could display a “software” mouse cursor by hiding the mouse cursor and moving a Sprite 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.

If you have to use the “software” approach, consider adding an extrapolation step to better display the actual mouse input.

使用项目设置

打开项目设置,转到 Display>Mouse Cursor。您将看到自定义图像和自定义图像热点。

../../_images/cursor_project_settings.png

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

注解

自定义图像 必须 小于256x256。

使用脚本

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

GDScript

C#

  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().

演示项目

通过研究这个演示项目了解更多信息:https://github.com/guilhermefelipecgs/custom_hardware_cursor

光标列表

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