Version: 2022.2

CursorLockMode

enumeration

切换到手册

描述

光标的行为方式。

这些是控制 Cursor 行为的各种模式。必须在 **PlayerSettings** > **Default Cursor** 中设置默认光标。

//This script makes Buttons that control the Cursor's lock state. Note that the Confined mode only works on Windows and Linux Standalone platform build.

using UnityEngine;

public class Example : MonoBehaviour { void Update() { //Press the space bar to apply no locking to the Cursor if (Input.GetKey(KeyCode.Space)) Cursor.lockState = CursorLockMode.None; }

void OnGUI() { //Press this button to lock the Cursor if (GUI.Button(new Rect(0, 0, 100, 50), "Lock Cursor")) { Cursor.lockState = CursorLockMode.Locked; }

//Press this button to confine the Cursor within the screen if (GUI.Button(new Rect(125, 0, 100, 50), "Confine Cursor")) { Cursor.lockState = CursorLockMode.Confined; } } }

变量

None光标行为未修改。
LockedLocks the cursor to the center of the Game view.
Confined将光标限制在游戏窗口中。