AutoClickButton
An AutoClickButton is a Button that clicks itself after Delay seconds, such as "Continue" on a result screen. A bar at the bottom fills while the time runs.
<tb:AutoClickButton text="Continue" delay="5" />
var next = root.Q<AutoClickButton>();
next.clicked += LoadNextLevel;
next.RegisterCallback<PointerEnterEvent>(_ => next.Paused = true);
next.RegisterCallback<PointerLeaveEvent>(_ => next.Paused = false);
| Member | Description |
|---|---|
Delay (delay) |
Seconds until the button clicks itself (3). |
Paused (paused) |
Stops the time while true. The bar keeps its place. |
AutoStart (auto-start) |
True (the default): the count starts when the button first attaches to a panel. False: call Restart. |
Restart(), Cancel() |
Starts the count again from 0, also after a click. Stops the count without a click. |
DrainRate (drain-rate) |
The part of the progress that Drain takes away each second, for example 0.5 for a full bar in 2 seconds. Default 0. |
Drain(), Continue() |
Stops the count without a click and lowers the progress by DrainRate each second until it is 0. With DrainRate 0, Drain is Cancel. Continue starts the count from the progress of now. After a click, Continue is Restart. |
IsDraining |
True while Drain lowers the progress. |
IsCounting, IsDone |
True while the time runs. True after a click, by the user or by the time, until Restart. |
ClickedByTime |
True when the last click came from the time, false when the user was first. |
Elapsed, Progress, SecondsLeft |
The counted seconds, the part of Delay that has gone (0 to 1), and the whole seconds left, for a "Continue (3)" text. After a click, Elapsed and Progress keep the time of the click. |
- A click by the user, or the time, clicks once. The time click is a
NavigationSubmitEvent, so theclickedevent and the click sound ofWorkspaceAudiofollow. - The time is real time, so a paused game (
Time.timeScale0) still counts. The time stops while the button is not on a panel. A disabled button does not click.
Quick time events
The button is also a prompt with a time limit. ClickedByTime tells a hit from a miss, and Elapsed is the reaction time.
prompt.AutoStart = false;
prompt.Delay = 1.5f;
prompt.clicked += () => status.text = prompt.ClickedByTime ? "Too slow" : $"Hit in {prompt.Elapsed:0.00} s";
prompt.Restart(); // shows the prompt and starts the time
For a hold, call Restart() on the press and Cancel() on the release. The click of the time is then the end of the hold. A ProgressRing can show Progress in place of the bar.
For a hold that does not start over, set DrainRate. Call Continue() on the press and Drain() on the release. An early release then lowers the progress slowly, and the next press goes on from the progress of that time.
<tb:AutoClickButton name="hold" auto-start="false" delay="1.5" drain-rate="0.5" />
// The press goes on from the progress, the release drains it
hold.DrainRate = 0.5f; // a full bar drains in 2 seconds
// On the press: if (!hold.IsCounting) hold.Continue();
// On the release: if (hold.IsCounting) hold.Drain();
- A pause stops the drain too. The drain stops at 0 and does not click.
The InputDemo example scene has a timed press, a hold with a ring (reset on release, or drain slowly) and a fast tap, with a key look and a gamepad look.
Styling
| Class | Element |
|---|---|
tb-auto-click-button |
The button. It has the look of the MessageBox buttons. The sheet hides its overflow, so the bar stays inside the corners. |
tb-auto-click-button__text |
The text of the button, a TextElement child. text reads and writes it. |
tb-auto-click-button__progress |
The bar at the bottom, 3 px, in --tb-color-accent. The script sets its width. |
tb-auto-click-button--done |
On the button after a click. The sheet hides the bar. |