728x90
반응형
1. Input 클래스
1.1 마우스 버튼
(1) 함수
함수 | 설명 |
bool GetMouseButtonDown (int button); | 마우스 버튼을 누른 프레임 동안 true를 반환 |
bool GetMouseButton (int button); | 마우스 버튼이 눌렸는지 여부를 반환 |
bool GetMouseButtonUp (int button); | 마우스 버튼에서 손을 뗏을 때 true를 반환 |
(2) 매개 변수 (int button)
매개변수 | 설명 |
0 | 마우스 왼쪽 버튼 |
1 | 마우스 오른쪽 버튼 |
2 | 마우스 휠 버튼 |
3 ~ 6 | 마우스 추가 버튼 |
(3) 데모
using UnityEngine;
public class DemoMouseInputClass : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("마우스 왼쪽 버튼 다운");
}
if (Input.GetMouseButton(0))
{
Debug.Log("마우스 왼쪽 버튼 클릭 중");
}
if (Input.GetMouseButtonUp(0))
{
Debug.Log("마우스 왼쪽 버튼 클릭 업");
}
}
}
1.2 마우스 휠
함수 | 설명 |
Input.GetAxis("Mouse ScrollWheel") | 값이 음수이면 휠 밀어 올리는 것이고, 양수이면 휠을 당겨 내리는 것임 |
Input.mouseScrollDelta.y | 값이 음수이면 휠 밀어 올리는 것이고, 양수이면 휠을 당겨 내리는 것임 |
using UnityEngine;
public class DemoMouseWheel : MonoBehaviour
{
void Update()
{
float mouseWheel1 = Input.GetAxis("Mouse ScrollWheel");
if (mouseWheel1 > 0)
{
Debug.Log("휠 밀어 올림");
}
else if (mouseWheel1 < 0)
{
Debug.Log("휠 당겨 내림");
}
Vector2 mouseWheel2 = Input.mouseScrollDelta;
if (mouseWheel2.y > 0)
{
Debug.Log("휠 밀어 올림");
}
else if (mouseWheel2.y < 0)
{
Debug.Log("휠 당겨 내림");
}
}
}
1.3 마우스 위치
[함수] Vector3 Input.mousePosition;
픽셀 좌표에서의 현재 마우스 위치를 나타냅니다.(읽기전용)
화면 또는 창의 왼쪽 아래부분이 (0,0)을 나타냅니다.
화면 또는 창의 오른쪽 위 부분은 (Screen.width, Screen.height)를 나타냅니다.
using UnityEngine;
public class DemoMousePosition : MonoBehaviour
{
void Update()
{
Debug.Log($"Mouse X : {Input.mousePosition.x}, Mouse Y : {Input.mousePosition.y}");
}
}
2. Interface(인터페이스)
인터페이스를 사용하여, 마우스 이벤트를 처리하는 것입니다.
인터페이스는 클래스 선언 시 인터페이스도 상속하여야 합니다.
(1) 네임스페이스
네임스페이스 |
using UnityEngine.EventSystems |
(2) 드래그 앤드 드롭 (Drop And Drag) 관련
인터페이스 | 함수 | 내용 |
IBeginDragHandler | OnBeginDrag | 드래그가 시작되기 전에 BaseInputModule에 의해 호출됩니다. |
IDragHandler | OnDrag | 드래그가 발생하는 경우, 커서가 움직일 때마다 호출됩니다. |
IDropHandler | OnDrop | 드롭을 받을 수 있는 대상에서의 BaseInputModule에 의해 호출됩니다. |
IEndDragHandler | OnEndDrag | 드래그 종료 시 BaseInputModule에 의해 호출됩니다. |
(3) OnPointer 관련
인터페이스 | 함수 | 내용 |
IPointerClickHandler | OnPointerClick | 마우스 클릭 또는 터치시 이벤트 발생합니다. |
IPointerDownHandler | OnPointerDown | 마우스 다운 또는 터치시 이벤트 발생합니다. |
IPointerEnterHandler | OnPointerEnter | 마우스 포인터가 범위안에 들어 올 때 이벤트 발생합니다. |
IPointerExitHandler | OnPointerExit | 마우스 포인터가 범위밖에 나갈 때 이벤트 발생합니다. |
IPointerUpHandler | OnPointerUp | 마우스 업 또는 터치 업시 이벤트 발생합니다. |
(4) 데모
using UnityEngine;
using UnityEngine.EventSystems;
public class DemoMouseInterface : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("영역 안");
}
public void OnPointerExit(PointerEventData data)
{
Debug.Log("영역 밖");
}
}
3. OnMouse
함수 | 설명 |
OnMouseDown | OnMouseDown은 사용자가 GUIElement 또는 Collider위에서 마우스 버튼을 누른 경우에, 호출. |
OnMouseDrag | OnMouseDrag는 사용자가 GUIElement 또는 Collider를 클릭하고 마우스 버튼을 계속 누르고 있는 경우에 호출 |
OnMouseEnter | 마우스가 GUIElement 또는 Collider로 들어갔을 때 호출. |
OnMouseExit | 마우스가 더이상 GUIElement 또는 Collider 위에 없는 경우에 호출 |
OnMouseOver | 마우스가 GUIElement 또는 Collider 위에 있는 동안, 매 프레임 마다 호출 |
OnMouseUp | OnMouseUp은 사용자가 마우스 버튼 클릭을 해제했을 때 호출 |
OnMouseUpAsButton | OnMouseUpAsButton는 GUIElement 또는 Collider에서 마우스 버튼 클릭이 해제되었을 때에만 호출 |
using UnityEngine;
public class DemoOnMouse : MonoBehaviour
{
void OnMouseEnter()
{
Debug.Log("영역으로 들어감");
}
void OnMouseOver()
{
Debug.Log("영역 안");
}
void OnMouseExit()
{
Debug.Log("영역에서 나감");
}
}
728x90
반응형
'프로그램 > 유니티 스크립트 소스' 카테고리의 다른 글
[유니티 스크립트 소스] 화면 캡처 png, jpg, tga, exr 파일로 저장 (0) | 2020.05.08 |
---|---|
[유니티 스크립트 소스] 배열에서 최소값, 최소값 얻기 (0) | 2020.04.14 |
[유니티 스크립트 소스] 마우스로 게임오브젝트 Drag로 이동시키기 (1) | 2020.03.15 |
[유니티 스크립트 소스] Struct ↔ Byte 배열 (0) | 2020.03.12 |
[유니티 스크립트 소스] XKCDColors (0) | 2020.03.10 |