【发布时间】:2013-06-18 20:43:37
【问题描述】:
我希望有人可以帮助我解决一个小问题。
目前我有一个连接到主摄像头的输入管理器,允许用户通过将鼠标移动到窗口边缘来平移地图,但我遇到了一个小问题,我试图自己解决无济于事。
如果鼠标移到窗口之外,仍然会发生平移,当我在调试或使用其他应用程序时,我觉得这很烦人。所以我希望有人可以帮助我阻止鼠标在游戏窗口外时发生的移动。
这是我的输入管理器的代码。
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
public Vector3 position = new Vector3(0,0, -10);
public int boundary = 50;
public int speed = 4;
private int screenBoundsWidth;
private int screenBoundsHeight;
// Use this for initialization
void Start()
{
screenBoundsWidth = Screen.width;
screenBoundsHeight = Screen.height;
}
// Update is called once per frame
void Update()
{
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
if (Input.mousePosition.y > screenBoundsHeight - 10) {
position.y += speed * Time.deltaTime;
}
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
Camera.mainCamera.transform.position = position;
}
}
感谢您的宝贵时间。
编辑
我想出了一个巧妙的解决方法,但它仍然会导致移动发生在窗口外部周围的某些位置。我希望有人能提出更好的解决方案。
if (Input.mousePosition.x < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) {
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
}
if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) {
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
}
if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) {
if (Input.mousePosition.y > screenBoundsHeight - 22) {
position.y += speed * Time.deltaTime;
}
}
if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) {
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
}
【问题讨论】:
-
这是用于 WinForms 的吗?
-
不幸的是,它适用于 Unity3D,但我使用的是 C#。