Write custom interactions
You can also write a custom Interaction to use in your project. You can use custom Interactions in the UI and code the same way you use built-in Interactions.
Add a class implementing the IInputInteraction interface, like this:
// Interaction which performs when you quickly move an
// axis all the way from extreme to the other.
/// <summary>
/// Example custom interaction that performs when an axis moves quickly
/// from one extreme to the other.
/// </summary>
public class MyExampleInteraction : IInputInteraction
{
/// <summary>
/// Time window, in seconds, within which the axis must move from one extreme to the other.
/// </summary>
public float duration = 0.2f;
/// <summary>
/// Processes the current state of the control(s) the interaction is bound to.
/// </summary>
/// <param name="context">Context giving access to the control state and phase transition methods.</param>
public void Process(ref InputInteractionContext context)
{
if (context.timerHasExpired)
{
context.Canceled();
return;
}
switch (context.phase)
{
case InputActionPhase.Waiting:
if (context.ReadValue<float>() == 1)
{
context.Started();
context.SetTimeout(duration);
}
break;
case InputActionPhase.Started:
if (context.ReadValue<float>() == -1)
context.Performed();
break;
}
}
// Unlike processors, Interactions can be stateful, meaning that you can keep a
// local state that changes over time as input is received. The system might
// invoke the Reset() method to ask Interactions to reset to the local state
// at certain points.
/// <summary>
/// Resets the interaction's local state.
/// </summary>
public void Reset()
{
}
void Start()
{
InputSystem.RegisterInteraction<MyExampleInteraction>();
var Action = new InputAction(interactions: "MyExample(duration=0.5)");
}
}
Register your interaction with the Input System in your initialization code:
InputSystem.RegisterInteraction<MyExampleInteraction>();
Your new Interaction is now available in the Input Action Asset Editor window.
You can also add it in code using this call:
var Action = new InputAction(interactions: "MyExample(duration=0.5)");