유니티의 인풋시스템이 새로워졌다
1. 먼저 Package Manager를 이용해서 Input System을 다운로드 받는다 (다운로드 다 받으면 프로젝트를 새로 키라고 하는데 새로 Restart 해준다
2. Project Settings에 Active Input Handling을 Both로 해준다 (New, old 선택이 있지만 혹시 모를 오류)
3. 프로젝트 파일에 새로 만들기 해서 Input Actions를 추가해준다
4. 왼쪽 위 No Control Schemes를 클릭해서 스키마를 추가해준다
5. 스키마 이름은 아무렇게나 설정하고 입력기기는 Keyboard와 Mouse를 선택해준다 (단, +눌러 선택할 때 엔터키로 입력하자... 버그인지 마우스 클릭이 안먹힌다)
6. Action Maps에 +를 눌러서 PlayerActions라고 이름 바꾸고 Actions에 Moving이라고 이름을 짓는다(나중에 함수 호출할때 브로드캐스트같은거 하면 필요) 그리고 Moving의 Action Type과 Control Type을 value / Vector2로 수정한다
7. 저 Moving이라는 Actions에 +를 누르면 뜨는거 2번째것을 클릭하면 위 아래 왼쪽 오른쪽이 자동 생성된다
8. 이제 위 아래 왼쪽 오른쪽에 WASD로 바인딩을 해준다 (2022.3.13f1 이여서 그런지 모르겟으나 d가 가려져있음)
9. 바인딩 빈거 없애고 Auto-Save 체크 해주면 1차 끝이다
10. 이제 cube 오브젝트를 하나 만들고 거기에 PlayerInput을 추가시켜준다
11. 만들었던걸 추가시켜주고 만들었던 WindowPC로 만들어 주면된다
12. cs 코드 파일 하나 만들어서 "On" + "Actions이름"으로 함수 하나 만들어준다
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputManager : MonoBehaviour
{
private Vector3 m_moveDirection;
private float m_moveSpeed = 4f;
void Update(){
transform.Translate(m_moveDirection * m_moveSpeed * Time.deltaTime);
}
void OnMoving(InputValue _value){
Vector2 input = _value.Get<Vector2>();
if(input != null){
m_moveDirection = new Vector3(input.x, 0f, input.y);
}
}
}
이제 WASD로 무빙이 가능하다
주의점
행동의 SendMessages와 BroadcastMessages는 "On" + "Actions이름"으로 만든 함수가 실행된다
하지만 InvokeUnityEvents와 InvokeCSharpEvents로 만든 친구는 버튼에 함수를 넣는것 처럼 해야한다
그리고 매개변수 타입도 InputAction.CallbackContext 이걸로 해야 한다
밑에는매개변수를 수정한 함수를 하나 더 만들었다
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputManager : MonoBehaviour
{
private Vector3 m_moveDirection;
private float m_moveSpeed = 4f;
void Update(){
transform.Translate(m_moveDirection * m_moveSpeed * Time.deltaTime);
}
public void OnInvokeMoving(InputAction.CallbackContext _value){
Vector2 input = _value.ReadValue<Vector2>();
if(input != null){
m_moveDirection = new Vector3(input.x, 0f, input.y);
}
}
void OnMoving(InputValue _value){
Vector2 input = _value.Get<Vector2>();
if(input != null){
m_moveDirection = new Vector3(input.x, 0f, input.y);
}
}
}
버튼 마냥 추가~
Behavior를 SendMessages로 둿을때 처럼 Moving이 쌉가능 하다
'Unity' 카테고리의 다른 글
ObjectPool (0) | 2024.01.07 |
---|---|
유니티 사운드 매니저 (0) | 2023.12.10 |
AOSFogWar (0) | 2023.12.03 |
Dotween 맛보기 (0) | 2023.12.03 |
UGS 맛보기 (0) | 2023.11.20 |