Search Unity

Feedback Why generated c# class from inputactions asset is not a static?

Discussion in 'Input System' started by vizgl, Mar 28, 2021.

  1. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    51
    So, question is in the title.
    Let say, I generate class GameInput from inputactions asset. Now to use it I should create instance in the Player class.

    Code (CSharp):
    1. void Awake()
    2. {
    3.     _input = new GameInput();
    4. }
    Now, I need create same instance in the Tutorial behaviour, to know which input hint to show, based on active control scheme.
    Code (CSharp):
    1. class Tutorial: MonoBehaviour
    2. {
    3.     GameInput _input;
    4.  
    5.    void Awake()
    6.    {
    7.         _input = new GameInput();
    8.    }
    9. }
    10.  
    I should make same steps in the every place, where input is used. Why class GameInput is not a static? It's waste of memory and cpu time, to instantiate GameInput in the many places.
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    97
    Well you can always do it like this, if you prefer static

    class Tutorial: MonoBehaviour
    {
    static GameInput _input;
    void Awake()
    {
    if(_input == null) {
    _input = new GameInput();
    }
    }
    }
     
  3. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    51
    I made same thing, but little bit different. Create new cs file with content:
    Code (CSharp):
    1. public partial class GameInput
    2. {
    3.     private static GameInput _instance = null;
    4.  
    5.     public static GameInput Get
    6.     {
    7.         get
    8.         {
    9.             if (_instance == null)
    10.                 _instance = new GameInput();
    11.  
    12.             return _instance;
    13.         }
    14.     }
    15.  
    16.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    17.     static void ResetStatic()
    18.     {
    19.         _instance = null;
    20.     }
    21. }
    Now I use it like this GameInput.Get.Player.Movement.ReadValue<Vector2>()

    But, I still think, this workarounds is unnecessary(until Unity's team will prove opposite :)), because GameInput can be static by default
     
unityunity