Recursive AI

Add vibration feedback to a game button in C#

onegame is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

Request a light phone vibration when the player activates the button. Android and iOS can play it; desktop and web only log it. For controller rumble, use GamepadManager.Rumble instead.

Terminal
dotnet add package RecursiveAI.OneGame.Host

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneGame.Platform;
using OneGame.UI;
 
var confirm = Ui.Button("Confirm",
    Haptics.Tick);

Add confirm to the UI tree with ui.Root.Add(confirm), where ui is your UiTree. Each activation requests one Tick.

Unzip the example, then dotnet run.

The full program

This desktop example focuses the button and sends it the UI’s activate event, without a window or a phone. It checks the log for exactly one Tick and throws if the button isn’t focusable or doesn’t request the expected effect.

C#
using OneGame.Platform;
using OneGame.UI;
 
var confirm = Ui.Button("Confirm", Haptics.Tick);
var ui = new UiTree();
ui.SetViewport(320, 180);
ui.Root.Add(confirm);
ui.Frame();
if (!ui.Focus(confirm))
    throw new InvalidOperationException("Could not focus Confirm.");
 
Haptics.ClearLog();
ui.Dispatch(UiEvent.Activate());
var requests = Haptics.GetLog();
if (requests.Count != 1 || requests[0].Kind != HapticKind.Tick)
    throw new InvalidOperationException("Expected one Tick request.");
 
Console.WriteLine($"{requests[0].Kind} requests: {requests.Count}");
Console.WriteLine($"Delivered: {requests[0].Delivered}");
Text
Tick requests: 1
Delivered: False

Delivered: False is expected on desktop. The test checks that the button asked for feedback, not that a device vibrated.

Variations

Use a stronger pulse. Replace Haptics.Tick with Haptics.Buzz and check for HapticKind.Buzz in the test.

C#
var confirm = Ui.Button("Confirm",
    Haptics.Buzz);

On desktop, the example then prints Buzz requests: 1 and Delivered: False.

Build a game menu in C# · Navigate a game menu with a controller


The short answer

Put Haptics.Tick in the button’s click or activation callback. Android and iOS use the system’s touch feedback; the same callback can run on desktop where Haptics.GetLog() lets you check the request.

What are haptics in gaming?

Haptics are effects you feel, such as a small pulse when a button accepts a tap. Use a light Tick for an ordinary button and reserve Buzz for a stronger cue. Call it from the UI handler rather than once per frame while the button is down, so one activation gives one request. Don’t put the effect in simulation code that could run again during a replay or on a server.

This API is for phone touch feedback. It isn’t controller rumble: a plugged-in gamepad won’t vibrate from Haptics.Tick, even if that pad activated the button. For a gamepad, onegame has a separate GamepadManager.Rumble API.

How do I turn on haptic feedback in my game?

Haptics.Enabled defaults to true. Set it from your game’s vibration preference if you offer one. It controls whether the request is sent to the system; the request still appears in the log when the game setting is off. Android’s touch feedback uses the game window and doesn’t need a vibration permission. The system can still suppress feedback according to the user’s settings. iOS uses UIKit feedback generators; an iOS simulator won’t produce a physical pulse.

To check your button without a phone, isolate the activation as the example does: clear old requests, activate the button once, then check the kind and count. Be careful not to interpret the log’s Delivered field as a measurement of vibration. It records whether onegame handed the request to the phone’s system, which may still silence it. Checking the physical feel of the pulse requires a device.

All onegame recipes