유니티 심화

절대강좌 유니티 - Input System

다모아 2023. 8. 31. 12:01

Component - Player Input

 

#region

:코드 접기

 

SendMessage

Invoke Unity Events

Invoke C Sharp Events

 


WarriorController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class WarriorController : MonoBehaviour
{
    private Animator anim;
    private Vector3 moveDir;
    private Transform transform;

    private PlayerInput playerInput;
    private InputActionMap mainActionMap;
    private InputAction moveAction;
    private InputAction attackAction;
    private void Start()
    {
        this.anim = this.GetComponent<Animator>();
        this.transform = this.GetComponent<Transform>();
        
        this.playerInput = this.GetComponent<PlayerInput>();
        //ActionMap 추출
        this.mainActionMap = this.playerInput.actions.FindActionMap("PlayerActions");
        //Move, Attack 액션 추출
        this.moveAction = this.mainActionMap.FindAction("Move");
        this.attackAction = this.mainActionMap.FindAction("Attack");

        this.moveAction.performed += (context) => {
            //이동
            Vector2 dir = context.ReadValue<Vector2>();
            this.moveDir = new Vector3(dir.x, 0, dir.y);
            this.anim.SetFloat("Movement", dir.magnitude);
        };

        this.moveAction.canceled += (context) => {
            //멈추기
            this.moveDir = Vector3.zero;
            this.anim.SetFloat("Movement", 0);
        };

        //공격
        this.attackAction.performed += (context) => {

            this.anim.SetTrigger("Attack");
        };
    }

    private void Update()
    {
        if (this.moveDir != Vector3.zero)
        {
            this.transform.rotation = Quaternion.LookRotation(moveDir);
            this.transform.Translate(Vector3.forward * 1f * Time.deltaTime);
        }
    }

    #region SEND_MESSAGE
    //SendMessage일 경우
    //void On액션명
    void OnMove(InputValue value)
    {
        Vector2 dir = value.Get<Vector2>();
        Debug.LogFormat("dir: {0}", dir);
        this.moveDir = new Vector3(dir.x, 0, dir.y);

        this.anim.SetFloat("Movement", dir.magnitude);
    }

    void OnAttack()
    {
        Debug.Log("Attack");
        this.anim.SetTrigger("Attack");
    }
    #endregion

    #region UNITY_EVENTS
    //method overloading
    public void OnMove(InputAction.CallbackContext context)
    {
        Debug.LogFormat("context.phase: {0}", context.phase);

        Vector2 dir = context.ReadValue<Vector2>();
        this.moveDir = new Vector3(dir.x, 0, dir.y);
        this.anim.SetFloat("Movement", dir.magnitude);
    }

    public void OnAttack(InputAction.CallbackContext context)
    {
        Debug.LogFormat("context.phase: {0}", context.phase);

        if(context.performed)
        {
            Debug.Log("Attack");
            this.anim.SetTrigger("Attack");
        }
    }
    #endregion
}

 

WarriorDirectController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class WarriorDirectController : MonoBehaviour
{
    private InputAction moveAction;
    private InputAction attackAction;
    private Animator anim;
    private Vector3 moveDir;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        //1. 액션 만들기
        this.moveAction = new InputAction("Move", InputActionType.Value);
        this.attackAction = new InputAction("Attack", InputActionType.Button);

        //2. 바인딩
        //move action binding
        InputActionSetupExtensions.CompositeSyntax syntax = this.moveAction.AddCompositeBinding("2DVector");
        syntax = syntax.With("Up", "<Keyboard>/w");
        syntax = syntax.With("Down", "<Keyboard>/s");
        syntax = syntax.With("Left", "<Keyboard>/a");
        syntax = syntax.With("Right", "<Keyboard>/d");

        //attack action binding
        this.attackAction.AddBinding("<Keyboard>/space");

        //3. 이벤트 붙이기
        //이동
        this.moveAction.performed += (context) => { 
            Vector2 dir = context.ReadValue<Vector2>();
            this.moveDir = new Vector3(dir.x, 0, dir.y);
            //애니메이션 실행
            this.anim.SetFloat("Movement", dir.magnitude);
        };
        //멈춤
        this.moveAction.canceled += (context) => {
            this.moveDir = Vector3.zero;
            //애니메이션 실행
            this.anim.SetFloat("Movement", 0);
        };
        //공격
        this.attackAction.performed += (context) => {
            Debug.Log("Attack");
            //애니메이션 실행
            this.anim.SetTrigger("Attack");
        };

        //4. 활성화
        this.moveAction.Enable();
        this.attackAction.Enable();
    }

    // Update is called once per frame
    void Update()
    {
        if(this.moveDir != Vector3.zero)
        {
            //회전
            this.transform.rotation = Quaternion.LookRotation(this.moveDir.normalized);
            //이동
            this.transform.Translate(Vector3.forward * 3f * Time.deltaTime);
        }
    }
}