본문 바로가기
개발💻/Unity

[Unity] New input system 3 ( Behavior 설정 with code )

by Sports Entrepreneur 2024. 5. 18.

Behavior 설정

Behavior 옵션 설명
Send Messages 이 옵션을 선택하면, 입력 이벤트가 발생할 때 Unity의 기존 메시지 시스템을 통해 메시지가 전송됩니다. 주로 OnMove, OnLook, OnJump 등의 메시지를 게임 오브젝트에 보내는 방식입니다.
Invoke Unity Events 이 옵션은 Unity 이벤트 시스템을 사용하여 입력 이벤트를 트리거합니다. 사용자는 입력 액션에 연결된 Unity 이벤트를 설정하고, 입력이 발생할 때 해당 이벤트가 호출됩니다.
Broadcast Messages 이 옵션을 선택하면, 입력 이벤트가 발생할 때 게임 오브젝트 및 해당 오브젝트의 모든 자식에게 메시지가 전송됩니다. 이는 Send Messages 옵션보다 더 광범위하게 메시지를 전파합니다.
Invoke C# Events 이 옵션은 C# 이벤트를 사용하여 입력 이벤트를 트리거합니다. 개발자는 입력 액션에 C# 이벤트 핸들러를 직접 연결할 수 있으며, 입력 이벤트가 발생할 때 해당 핸들러가 호출됩니다.

 

 

  • Send Messages 예시 코드
using UnityEngine;
using UnityEngine.InputSystem;

public class SendMessageExample : MonoBehaviour
{
    public void OnJump()
    {
        Debug.Log("Jump message received!");
    }
}

public class PlayerController : MonoBehaviour
{
    private void OnEnable()
    {
        var input = new PlayerInput();
        input.Player.Jump.performed += ctx => SendMessage("OnJump");
        input.Enable();
    }
}



  • Invoke Unity Events 예시 코드
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;

public class UnityEventExample : MonoBehaviour
{
    public UnityEvent onJump;

    private void OnEnable()
    {
        var input = new PlayerInput();
        input.Player.Jump.performed += ctx => onJump.Invoke();
        input.Enable();
    }
}

 

 

  • Broadcast Messages
using UnityEngine;
using UnityEngine.InputSystem;

public class BroadcastMessageExample : MonoBehaviour
{
    public void OnJump()
    {
        Debug.Log("Jump broadcast message received!");
    }
}

public class PlayerController : MonoBehaviour
{
    private void OnEnable()
    {
        var input = new PlayerInput();
        input.Player.Jump.performed += ctx => BroadcastMessage("OnJump");
        input.Enable();
    }
}

 

 

  • Invoke C# Events
using UnityEngine;
using UnityEngine.InputSystem;
using System;

public class CSharpEventExample : MonoBehaviour
{
    public event Action OnJumpEvent;

    private void Start()
    {
        OnJumpEvent += () => Debug.Log("Jump C# event received!");
    }

    private void OnEnable()
    {
        var input = new PlayerInput();
        input.Player.Jump.performed += ctx => OnJumpEvent?.Invoke();
        input.Enable();
    }
}