virtual_button.gd (18.5 KB)
※Updated on 2026/6/4.
Fixed so that coordinates do not shift when scaling.
Button Image Sample.zip (2.5 KB)
VirtualButton is a script to attach to a Node2D. It allows tapping or clicking a visual node on the screen to register as pressing a specified action in the InputMap.
On the GameObject side, this can be detected via input linking or signals.
Steps for the simplest configuration
As an example, here is how to create a jump button for an action named “jump”.
-
Open the Input Map in Project Settings and add an action named “jump”.
-
Add a VirtualButton node to the scene.
You can either add a Node2D and attach VirtualButton.gd, or create a VirtualButton directly via Scene → + button. -
Prepare the hitbox and visual appearance.
Select the VirtualButton node and press the button labeled “Generate and assign default child nodes” in the ≪Reference Node≫ section of the inspector.
(※If the button does not work, please save the project, close it, and reopen it.)
This will automatically assign:
- hitbox_control: a Control node named ButtonArea
- visual_node: a TextureRect node named ButtonTexture
From now on, the ButtonArea range will be used for click/tap detection on screen, and ButtonTexture will serve as the button’s visual appearance.
-
Enter “jump” into the input_action_name field of the VirtualButton node.
Now, when this button is pressed, it will register as pressing the “jump” action in the InputMap. -
Set the texture for the visual element.
Assign the texture you want to display normally to texture_state_normal on the VirtualButton node.
If you want to change the appearance while the button is pressed, assign a different texture to texture_state_pressed.
To change the appearance when disabled, assign a texture to texture_state_disabled, though you can leave it empty if unused.
As long as enable_texture_swap remains true, the texture will automatically switch based on the pressed state and is_button_enabled.
-
Configure the target object’s .vs file.
Once these settings are complete, clicking with the left mouse button or tapping within the ButtonArea range will register as pressing the “jump” action.
Advanced: Whether to use signals
If you wish to use signals, connect generic_signal to the receive_signal property of the target GameObject.
If the InputMap alone is sufficient, no signal configuration is needed.
