본문 바로가기

유니티/유니티 엔진

[유니티] 키보드, 마우스 입력 처리 함수

728x90
반응형

 Input Class

 

유니티에서 제공하는 Input 클래스를 이용하여 키보드, 마우스의 입력을 받을 수 있다.

 

 

 

 

 키보드 입력

 

Input 클래스의 GetKey 메서드를 사용하면 해당 키가 눌렸을 때 True 값을 반환받을 수 있다.

GetKey 메서드는 키가 눌려있는 동안 True를 반환하고

GetKeyDown 메서드는 키가 눌릴 때 True를 반환하고

GetKeyUp 메서드는 키를 뗄 때 True를 반환한다.

위 메서드들은 해당하는 상황이 아닐 때에는 항상 False 를 반환한다.

따라서 아래 코드를 실행시켜보면 'w' 를 누를 때에는 True 가 출력되며

그렇지 않을 때에는 False 가 출력된다.

 

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

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
                                              
    // Update is called once per frame
    void Update()
    {
        Debug.Log(Input.GetKey(KeyCode.W));
    }
}

 

 

 

 

 

 

 

 

 

 마우스 입력

 

마우스 클릭 입력은 Input 클래스의 GetMouseButton 메서드를 사용하여 받을 수 있다.

0은 좌클릭, 1은 우클릭, 2는 휠을 나타낸다.

GetKey 메서드와 마찬가지로 마우스가 눌려있는 동안 True를 반환하고 눌려있지 않으면 False 를 반환한다.

GetMouseButtonDown, GetMouseButtonUp 메서드도 마찬가지이다.

따라서 아래 코드를 실행시켜보면 마우스 좌측 버튼을 누를 때에는 True 가 출력되며

그렇지 않을 때에는 False 가 출력된다.

 

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

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
                                              
    // Update is called once per frame
    void Update()
    {
        Debug.Log(Input.GetMouseButton(0));
    }
}

 

 

728x90
반응형