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。

使用脚本

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

extends Node

# Load the custom images for the mouse cursor.
var arrow = load("res://arrow.png")
var beam = load("res://beam.png")

func _ready():
    # Changes only the arrow shape of the cursor.
    # This is similar to changing it in the project settings.
    Input.set_custom_mouse_cursor(arrow)

    # Changes a specific shape of the cursor (here, the I-beam shape).
    Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)
public override void _Ready()
{
    // Load the custom images for the mouse cursor.
    var arrow = ResourceLoader.Load("res://arrow.png");
    var beam = ResourceLoader.Load("res://beam.png");

    // Changes only the arrow shape of the cursor.
    // This is similar to changing it in the project settings.
    Input.SetCustomMouseCursor(arrow);

    // Changes a specific shape of the cursor (here, the I-beam shape).
    Input.SetCustomMouseCursor(beam, Input.CursorShape.Ibeam);
}

演示项目

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

光标列表

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