Skip to content

Commit 67e120a

Browse files
authored
DOCS: Add Device Command example for simulated devices (#1568)
1 parent 1a958ab commit 67e120a

File tree

1 file changed

+35
-3
lines changed
  • Packages/com.unity.inputsystem/Documentation~

1 file changed

+35
-3
lines changed

Packages/com.unity.inputsystem/Documentation~/Devices.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,38 @@ public class MyDevice : InputDevice, IInputCallbackReceiver
547547
}
548548
```
549549

550-
[//]: # (#### Step 6: Device Commands (Optional))
551-
[//]: # (A final, but optional, step is to add support for Device commands. A "device command" is that opposite of input. In other words, it consists of data traveling __to__ the input device, which might also return data as part of the operation (much like a function call). You can use this to communicate with the backend of the device in order to query configuration, or to initiate effects such as haptics.)
552-
[//]: # (TODO: ATM we're missing an overridable method to make this work)
550+
#### Step 6: Device Commands (Optional)
551+
552+
A final, but optional, step is to add support for Device commands. A "device command" is that opposite of input. In other words, it consists of data traveling __to__ the input device, which might also return data as part of the operation (much like a function call). You can use this to communicate with the backend of the device in order to query configuration, or to initiate effects such as haptics. At the moment there isn't a proper interface available for this, however there are still some scenarios that can be solved with the current interfaces.
553+
554+
E.g. the following shows, when implementing a non-hardware-backed device (simulated device), how to simulate hardware reporting that the device can be run in the background and supports sync commands. This is useful to prevent the device from cancelling Actions when application focus is lost and restored. For more info see [Device syncs](#device-syncs)
555+
556+
```CSharp
557+
public class MyDevice : InputDevice, IInputCallbackReceiver
558+
{
559+
//...
560+
561+
protected override unsafe long ExecuteCommand(InputDeviceCommand* commandPtr)
562+
{
563+
var type = commandPtr->type;
564+
if (type == RequestSyncCommand.Type)
565+
{
566+
// Report that the device supports the sync command and has handled it.
567+
// This will prevent device reset during focus changes.
568+
result = InputDeviceCommand.GenericSuccess;
569+
return true;
570+
}
571+
572+
if (type == QueryCanRunInBackground.Type)
573+
{
574+
// Notify that the device supports running in the background.
575+
((QueryCanRunInBackground*)commandPtr)->canRunInBackground = true;
576+
result = InputDeviceCommand.GenericSuccess;
577+
return true;
578+
}
579+
580+
result = default;
581+
return false;
582+
}
583+
}
584+
```

0 commit comments

Comments
 (0)