返回介绍

Cursor.lockState 锁定状态

发布于 2019-12-18 15:37:38 字数 5464 浏览 1181 评论 0 收藏 0

JavaScript => public static var lockState: CursorLockMode;
C# => public static CursorLockMode lockState;

Description 描述

How should the cursor be handled?

应该怎样处理光标?

When locked, the cursor will automatically be centered on view and made to never leave the view. This will primarily be used with Cursor.visible = false. When confined, the cursor behave normally with the exception of being confined to the view.

当锁定时,光标将自动居视图中间,并使其从不离开视图。这将主要用于Cursor.visible = false,隐藏光标。当被限制时,光标的行为通常会被限制在视图中心。

In the web player, the cursor may only be locked/confined after the user has clicked on the content and the user has not left the content view with the cursor. After the user presses escape or switches to another application the cursor will be automatically reset to normal. The cursor state will also be lost when exiting full screen mode.

在web播放器,光标仅在用户点击内容之后锁定,并使用户光标不离开内容。在用户按下esc键或切换到其他应用程序之后,光标将自动恢复为正常。当退出全屏模式,光标状态也将会丢失。

To provide a good user experience it is recommended to only lock or confine the cursor as a result of pressing a button. Also you should check if the cursor was reset, in order to e.g. pause the game or bring up a game menu. In the Web Player and Editor the cursor will automatically be reset when you press escape. In the Standalone Player you have full control over the mouse cursor, thus it won't automatically be reset unless you switch applications.

为提供更好的用户体验,推荐点一个按键锁定光标,为了使游戏暂停或弹出游戏菜单,还应该检查光标是否要重置。在web播放器和编辑器,当按下esc键光标将自动重置。在独立播放器完全控制鼠标光标,因此,它不会自动重置,除非你的切换应用。

JavaScript:

#pragma strict
var wantedMode: CursorLockMode;
// Apply requested cursor state
function SetCursorState() {
	Cursor.lockState = wantedMode;
	// Hide cursor when locking
	Cursor.visible = (CursorLockMode.Locked != wantedMode);
}
function OnGUI() {
	GUILayout.BeginVertical();
	// Release cursor on escape keypress
	if (Input.GetKeyDown(KeyCode.Escape))
		Cursor.lockState = wantedMode = CursorLockMode.None;
	switch (Cursor.lockState) {
		case CursorLockMode.None:
			GUILayout.Label("Cursor is normal");
		if (GUILayout.Button("Lock cursor"))
			wantedMode = CursorLockMode.Locked;
		if (GUILayout.Button("Confine cursor"))
			wantedMode = CursorLockMode.Confined;
 
			break;
		case CursorLockMode.Confined:
			GUILayout.Label("Cursor is confined");
		if (GUILayout.Button("Lock cursor"))
			wantedMode = CursorLockMode.Locked;
		if (GUILayout.Button("Release cursor"))
			wantedMode = CursorLockMode.None;
 
			break;
		case CursorLockMode.Locked:
			GUILayout.Label("Cursor is locked");
		if (GUILayout.Button("Unlock cursor"))
			wantedMode = CursorLockMode.None;
		if (GUILayout.Button("Confine cursor"))
			wantedMode = CursorLockMode.Confined;
 
			break;
	}
	GUILayout.EndVertical();
	SetCursorState();
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
	CursorLockMode wantedMode;
 
	// Apply requested cursor state
	void SetCursorState ()
	{
		Cursor.lockState = wantedMode;
		// Hide cursor when locking
		Cursor.visible = (CursorLockMode.Locked != wantedMode);
	}
 
	void OnGUI ()
	{
		GUILayout.BeginVertical ();
		// Release cursor on escape keypress
		if (Input.GetKeyDown (KeyCode.Escape))
			Cursor.lockState = wantedMode = CursorLockMode.None;
 
		switch (Cursor.lockState)
		{
			case CursorLockMode.None:
				GUILayout.Label ("Cursor is normal");
				if (GUILayout.Button ("Lock cursor"))
					wantedMode = CursorLockMode.Locked;
				if (GUILayout.Button ("Confine cursor"))
					wantedMode = CursorLockMode.Confined;
				break;
			case CursorLockMode.Confined:
				GUILayout.Label ("Cursor is confined");
				if (GUILayout.Button ("Lock cursor"))
					wantedMode = CursorLockMode.Locked;
				if (GUILayout.Button ("Release cursor"))
					wantedMode = CursorLockMode.None;
				break;
			case CursorLockMode.Locked:
				GUILayout.Label ("Cursor is locked");
				if (GUILayout.Button ("Unlock cursor"))
					wantedMode = CursorLockMode.None;
				if (GUILayout.Button ("Confine cursor"))
					wantedMode = CursorLockMode.Confined;
				break;
		}
 
		GUILayout.EndVertical ();
 
		SetCursorState ();
	}
}

Cursor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文