CodeBlanks Logo Icon CodeBlanks

C# generated class

Learn to read actions with the auto-generated C# class.

The Generate C# Class option

If you tick Generate C# Class in the Input Action asset's Inspector, Unity produces a C# class named after the asset. The generated class exposes actions as properties; for example inputActions.Player.Jump is an InputAction object.

PlayerInputActions inputActions = new PlayerInputActions();
inputActions.Player.Enable();
Debug.Log("Eylemler aktif");

Attaching a method to its performed event with += gives you a compile-time checked wrapper.

started, performed and canceled

An action has three events: started when the input begins, performed when the action really fires, and canceled when the input is released. You add the subscription in OnEnable and remove it in OnDisable, so the links are cleaned up when the object is disabled.

void OnEnable()
{
    inputActions.Player.Jump.performed += OnJump;
}

void OnDisable()
{
    inputActions.Player.Jump.performed -= OnJump;
}

The event method takes an InputAction.CallbackContext.

Practice Set up the subscription

Write a MonoBehaviour named OyuncuKontrol. Add a PlayerInputActions field called inputActions and create it in Awake with new PlayerInputActions(). In OnEnable attach the OnJump method to the inputActions.Player.Jump.performed event; in OnDisable remove the same link. The OnJump method takes an InputAction.CallbackContext ctx and calls Debug.Log("Zipla!"). In Start call inputActions.Player.Enable() and print Debug.Log("Eylemler aktif").

Starter code

Player.cs
using UnityEngine;
using UnityEngine.InputSystem;

public class OyuncuKontrol : MonoBehaviour
{
    PlayerInputActions inputActions;

    void Awake()
    {
        // inputActions alanini PlayerInputActions ornegiyle doldur
    }

    // OnEnable icinde Jump.performed olayina OnJump metodunu ekle

    // OnDisable icinde ayni baglantiyi geri al

    // OnJump metodu CallbackContext alir ve ziplama mesaji basar

    void Start()
    {
        // Player eylem haritasini etkinlestir ve basari mesajini yazdir
    }
}

Sample solution

Player.cs
using UnityEngine;
using UnityEngine.InputSystem;

public class OyuncuKontrol : MonoBehaviour
{
    PlayerInputActions inputActions;

    void Awake()
    {
        inputActions = new PlayerInputActions();
    }

    void OnEnable()
    {
        inputActions.Player.Jump.performed += OnJump;
    }

    void OnDisable()
    {
        inputActions.Player.Jump.performed -= OnJump;
    }

    void OnJump(InputAction.CallbackContext ctx)
    {
        Debug.Log("Zipla!");
    }

    void Start()
    {
        inputActions.Player.Enable();
        Debug.Log("Eylemler aktif");
    }
}

Expected output

Eylemler aktif

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How do you subscribe to an action's event in the generated class?

  • inputActions.Player.Jump.performed += OnJumpMethod Correct answer
  • InputAction.actionFound += OnJumpMethod
  • Jump.OnPress += OnJumpMethod
  • playerInput.OnJump += OnJumpMethod

Explanation: In the generated class every action is an InputAction; the performed, started and canceled events accept methods with +=.

2 Remove the subscription

Question
inputActions.Player.Jump.performed ____;
Answer
inputActions.Player.Jump.performed -= OnJump;

Explanation: In OnDisable the subscription is removed with -=; += adds a new subscription and the old link stays in place.

3 Generated class concepts

Term Meaning
Generate C# Class The option that produces a C# class
Wrapper Class exposing actions as properties
performed The moment the action fires
CallbackContext The data the event method receives

All lessons in Advanced