Unity 게임을 만들다 보면, 마우스가 화면에 고정되어 있지 않아 테스트 하기에 불편함이 생긴다.
Unity에서 제공하는 마우스 잠금 상태를 변경하면 이러한 문제를 해결할 수 있다.
1. Cursor.lockState
Cursor.lockState: 마우스 커서의 잠금 상태를 나타내는 변수이다.
잠금 상태는 총 3가지로 CursorLockMode.Locked, CursorLockMode.Confined, CursorLockMode.None이 있다.
1) CursorLockMode.Locked: 마우스 커서를 게임 화면의 정중앙에 고정시키고 커서를 숨긴다. 게임 실행 후 화면을 클릭해야 적용된다.
2) CursorLockMode.Confined: 마우스 커서를 게임 화면 밖으로 나가지 않게 만든다.
*Windows와 LinuxOS에서만 되며 MaxOS에서는 지원하지 않는다.
3) CursorLockMode.None: 마우스 커서에 제한을 두지 않는다.
사용 방법은 아래 코드와 같다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
enum CursorLockState { Lock, Confine, None};
[SerializeField] CursorLockState _cursorLock = CursorLockState.Lock;
void Start()
{
switch (_cursorLock)
{
case CursorLockState.Lock:
Cursor.lockState = CursorLockMode.Locked; break;
case CursorLockState.Confine:
Cursor.lockState = CursorLockMode.Confined; break;
case CursorLockState.None:
Cursor.lockState = CursorLockMode.None; break;
default:
break;
}
}
}
2. Cursor.visible
Cursor.visible: 마우스 커서를 화면에 보일건지 정하는 변수이다.
visible이 true면 보이고, false면 숨기게 된다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
[SerializeField] bool _setCursorVisble = false;
private void Start()
{
Cursor.visible = _setCursorVisible;
}
}
참고 링크
https://docs.unity3d.com/ScriptReference/Cursor-visible.html
Unity - Scripting API: Cursor.visible
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close
docs.unity3d.com
https://docs.unity3d.com/ScriptReference/Cursor-lockState.html
Unity - Scripting API: Cursor.lockState
Description Determines whether the hardware pointer is locked to the center of the view, constrained to the window, or not constrained at all. A locked cursor is positioned in the center of the view and cannot be moved. The cursor is invisible in this stat
docs.unity3d.com
'게임개발 > Unity' 카테고리의 다른 글
[Unity] Button OnClick Event is triggered twice (0) | 2024.08.26 |
---|---|
표준국어대사전 API 이용 (Unity, C#) (0) | 2022.08.19 |
Unity User SSA CL Certificate Error (0) | 2022.08.15 |