Screen.lockCursor 锁定光标

static var lockCursor : bool

Description描述

Should the cursor be locked?

是否光标被锁定?

The cursor will automatically be hidden, centered on view and made to never leave the view.

光标将自动隐藏,居中于视图并且不会离开这个视图。

In the web player the cursor may only only be locked 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 unlocked. The cursor lock will also be lost when exiting full screen mode. You can query if the cursor is currently locked by checking the lockCursor state. To provide a good user experience it is recommended to only lock the cursor as a result of pressing a button. Also you should check if the cursor got unlocked, 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 unlocked when you press escape. In the Standalone Player you have full control over mouse locking thus it won't automatically lose mouse lock unless you switch applications.

在网络播放器中光标仅在用户点击内容并且用户光标没有离开内容视图时锁定。在用户按下Esc键或切换到其他应用程序之后,光标将被自动解锁。当推出全屏模式时,光标锁定也将丢失。当前光标是否被锁定,你可以通过lockCursor查询状态。为了提供一个良好的用户体验,建议只在用户按下按钮的时候锁定光标。你也应该检查光标是否解锁,例如暂停游戏或打开一个游戏菜单。在网络播放器和编辑器,当你按下Esc时,光标将自动被解锁。在独立游戏模式中,您拥有完全控制鼠标锁定,因此,它不会自动失去鼠标锁定,除非你切换应用程序。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void DidLockCursor() {
		Debug.Log("Locking cursor");
		guiTexture.enabled = false;
	}
	void DidUnlockCursor() {
		Debug.Log("Unlocking cursor");
		guiTexture.enabled = true;
	}
	void OnMouseDown() {
		Screen.lockCursor = true;
	}
	private bool wasLocked = false;
	void Update() {
		if (Input.GetKeyDown("escape"))
			Screen.lockCursor = false;

		if (!Screen.lockCursor && wasLocked) {
			wasLocked = false;
			DidUnlockCursor();
		} else
			if (Screen.lockCursor && !wasLocked) {
				wasLocked = true;
				DidLockCursor();
			}
	}
}
// Called when the cursor is actually being locked
//当光标被锁定时调用
function DidLockCursor () {
Debug.Log("Locking cursor");

// Disable the button
//禁用按钮
guiTexture.enabled = false;
}

// Called when the cursor is being unlocked
// or by a script calling Screen.lockCursor = false;
//当光标解锁时调用,或由一个脚本调用Screen.lockCursor = false;
function DidUnlockCursor () {
Debug.Log("Unlocking cursor");

// Show the button again
//再次显示按钮
guiTexture.enabled = true;
}

function OnMouseDown () {
// Lock the cursor
//锁定光标
Screen.lockCursor = true;
}

private var wasLocked = false;

function Update () {
	// In standalone player we have to provide our own key
	// input for unlocking the cursor
	//在独立游戏模式下我们需要提供自己键来解锁光标
	if (Input.GetKeyDown ("escape"))
		Screen.lockCursor = false;

	// Did we lose cursor locking? 
	//我们失去了光标锁定?
	// eg. because the user pressed escape 
	// or because he switched to another application
	// or because some script set Screen.lockCursor = false;
	//例如因为用户按下Esc或因为他切换了其他应用程序,或因为某些脚本设置了Screen.lockCursor = false;
	if (!Screen.lockCursor && wasLocked) {
		wasLocked = false;
		DidUnlockCursor();
	}
	// Did we gain cursor locking?
	//我们在次光标锁定?
	else if (Screen.lockCursor && !wasLocked) {
	wasLocked = true;
	DidLockCursor ();
	}
}
最后修改:2011年1月17日 Monday 21:54

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。