Skip to content

Instantly share code, notes, and snippets.

@mattdymott
Created December 6, 2023 20:28
Show Gist options
  • Save mattdymott/1da1bad575bb6ef951f72a953fce0fa3 to your computer and use it in GitHub Desktop.
Save mattdymott/1da1bad575bb6ef951f72a953fce0fa3 to your computer and use it in GitHub Desktop.
Example of using InputSystem with Unity Entities
// Managed IComponent
public class InputActionSingleton : IComponentData
{
// InputSystem actions asset wrapper which is auto generated by the asset.
// Has accessors for your Actions created in the asset.
public InputActions InputActions;
public InputActionSingleton()
{
InputActions = new InputActions();
InputActions.Enable();
}
public InputAction Move => InputActions.Gameplay.Move;
public InputAction Jump => InputActions.Gameplay.Jump;
}
// MoveInputAction and JumpInputAction could be combined into 1 struct.
public struct MoveInputAction : IComponentData
{
public float2 Axis;
}
public struct JumpInputAction : IComponentData
{
// Could use bit fields/states instead or just bytes.
public bool IsPressed;
public bool WasPressed;
public bool WasReleased;
}
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial struct InputActionSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
var entity = state.EntityManager.CreateSingleton(new InputActionSingleton());
state.EntityManager.AddComponent<MoveInputAction>(entity);
state.EntityManager.AddComponent<JumpInputAction>(entity);
}
// Can't be bursted.
public void OnUpdate(ref SystemState state)
{
// You could just use this singleton in any other system but it can't be bursted.
var inputActionSingleton = SystemAPI.ManagedAPI.GetSingleton<InputActionSingleton>();
var moveInputAction = SystemAPI.GetSingletonRW<MoveInputAction>();
var jumpInputAction = SystemAPI.GetSingletonRW<JumpInputAction>();
// Copy the data into the actions structs which can then be accessed from other systems
// and bursted.
moveInputAction.ValueRW.Axis = inputActionSingleton.Move.ReadValue<Vector2>();
jumpInputAction.ValueRW.IsPressed = inputActionSingleton.Jump.IsPressed();
jumpInputAction.ValueRW.WasPressed = inputActionSingleton.Jump.WasPressedThisFrame();
jumpInputAction.ValueRW.WasReleased = inputActionSingleton.Jump.WasReleasedThisFrame();
}
};
public partial struct PlayerInputSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<MoveInputAction>();
state.RequireForUpdate<JumpInputAction>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var moveInputAction = SystemAPI.GetSingleton<MoveInputAction>();
var jumpInputAction = SystemAPI.GetSingleton<JumpInputAction>();
// example character state
foreach(var characterState in SystemAPI.Query<RefRW<CharacterState>>()
.WithAll<PlayerCharacterTag>())
{
characterState.ValueRW.MoveVector = new float3(moveInputAction.Axis.x, 0f, moveInputAction.Axis.y);
characterState.ValueRW.JumpState = jumpInputAction;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment