[Unity] 마우스 커서 숨기기, 마우스 화면 고정(cursor visible, cursor lock)

2023. 12. 17. 13:11·게임개발/Unity

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] NetworkVariable, INetworkSerializable Synchronization Problem  (0) 2025.06.05
[Unity] StopCoroutine, 코루틴 중지시키기  (3) 2025.06.03
[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
'게임개발/Unity' 카테고리의 다른 글
  • [Unity] StopCoroutine, 코루틴 중지시키기
  • [Unity] Button OnClick Event is triggered twice
  • 표준국어대사전 API 이용 (Unity, C#)
  • Unity User SSA CL Certificate Error
깜냥c
깜냥c
게임 개발/클라이언트/AI/PS/기타 연구
  • 깜냥c
    Choice Program
    깜냥c
  • 전체
    오늘
    어제
    • 분류 전체보기 (57)
      • 언어 (11)
        • C,C++ (9)
        • C# (1)
        • Python (1)
      • PS (20)
        • 백준 문제 (19)
        • 알고리즘 (1)
      • 인공지능 (2)
      • 게임제작 (7)
      • 게임개발 (14)
        • Unity (7)
        • Unreal Engine (5)
        • Godot Engine (1)
      • 기타 (2)
  • 블로그 메뉴

    • 홈
    • 방명록
    • 블로그 소개
  • 링크

    • 김병장의 IT 블로그
    • 식품영양과 데이터사이언스
  • 공지사항

  • 인기 글

  • 태그

    백준
    Unreal
    BOJ
    Godot
    unity
    배낭 문제
    C언어
    입출력
    UE5
    C++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
깜냥c
[Unity] 마우스 커서 숨기기, 마우스 화면 고정(cursor visible, cursor lock)
상단으로

티스토리툴바