[내일 배움 캠프 Unity 4기] Final Project W12D2 07.02 TIL

김용준·2024년 7월 2일
0

내일배움캠프

목록 보기
41/47

Goals

  • Make the framework for input on the final project
  • Prepare the technical interview

Application of input system on the final project

In Unity, there is a input system which serve the large variety of input. Also there are some styles to apply 'Inputs' on the project. Basically, I learned the way to create input action asset and add the component 'Player Input' on the object. Studying until today, I suggested inconvenience on this method. Even though the easier method for beginner, I've preferred to control whole inputs via script. For that reason, I searched some other way to apply input on the player object.

1. Legacy

First time when I met the Unity, I learned the way to apply input at the Update method. The example could be seen in the following code snippet. Since I was a beginner, I didn't feel the inconvenience on that style. However, I grow up and see that the style contains a lot of problem.

public class PlayerInputManager : MonoBehaviour
{
  float dir; // direction; -1(left) or 1(right)
  void Update()
  {
    if(Input.GetKey(KeyCode.A))
      dir = -1;
    else if (Input.GetKey(KeyCode.D))
      dir = 1;
    else
      dir = 0;
      
      ... // Move methods
  }
}

2. Direct

Improving the legacy style, We can easily searched the input system tutorial. Among them, the basic input system has the example which contains this style. Even the style has enhanced from the legacy, I felt firm on the style. The example is shown on the bottom.

public class PlayerInputManager : MonoBehaviour
{
  float dir;
  Keyboard keyboard;
  Mouse mouse;
  Gamepad pad;
  
  void Start()
  {
    keyboard = Keyboard.current;
    Mouse = Mouse.current;
    Gamepad pad = Gamepad.current;
  }
  
  void Update()
  {
    if(keyboard.aKey.wasPressedThisFrame)
      dir = -1;
    else if (keyboard.dKey.wasPressedThisFrame)
      dir = 1;
    else
      dir = 0;
    ... // methods for other devices
  }
}

3. Embedded

The input can be separated into 3 part; begin of pressing, pressing, end of pressing. By facilitating delegate, Action on the C# and Unity, the expandancy was felt better than the previous styles. However there are plenty of studies should be done to apply this method, such as InputAction, delegate and Action<InputAction.CallbackContext> ... . With the Unity GUI, we can easily attach the input actions like the usage of asset of input action. The advantage of this style comes from the inspector window. By adding the input action as component will make you more comportable than other style. The example script is on the below code block.

public class PlayerInputManager : MonoBehaviour
{
  public InputAction moveAction; // which makes it serialize on the inspector
  void OnEnable()
  {
    moveAction.performed += PlayerMove;
    moveAction.canceled += PlayerStop;
    
    moveAction.Enable();
  }
  
  void OnDisable()
  {
    moveAction.performed -= PlayerMove;
    moveAction.canceled -= PlayerStop;
    
    moveAction.OnDisable();
  }
}
profile
꿈이큰개발자지망생

0개의 댓글