有没有办法统一将光标锁定在特定的屏幕位置?
Is there a way to lock the cursor on a specific screen position in unity?
基本上我想要一个点击并拖动相机,但不需要每次都将光标传送到屏幕中央。
到目前为止,我对相机移动的了解是:
public class CameraController : MonoBehaviour
{
Vector3 camRot = new Vector3(0, 0, 0);
Vector3 camPosRot = new Vector3(0, 0, 0);
public float VSpeed = 2.0f;
public float HSpeed = 2.0f;
public Vector3 CameraOffset = new Vector3(0,0,0);
public Rigidbody Follow;
public float Distance = 0.1f;
Vector3 CursorBC;
Vector3 CameraPos = new Vector3(0, 0, 0);
// Start is called before the first frame update
void Start()
{
//Follow = GetComponent<Rigidbody>();
CameraPos = CameraOffset;
}
// Update is called once per frame
void Update()
{
camRot += new Vector3(-Input.GetAxis("Mouse Y") * VSpeed, Input.GetAxis("Mouse X") * HSpeed, 0);
if (Input.GetKey(KeyCode.Mouse1))
{
CursorBC = Input.mousePosition;
Cursor.lockState = CursorLockMode.Locked;
transform.eulerAngles = camRot;
camPosRot = new Vector3(camRot.y,camRot.x);
Vector3 RotationVector = new Vector3(Mathf.Sin(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad), Mathf.Sin(camPosRot.y * Mathf.Deg2Rad), Mathf.Cos(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad));
CameraPos = new Vector3(CameraOffset.magnitude * -(RotationVector.x), CameraOffset.magnitude * (RotationVector.y), CameraOffset.magnitude * -(RotationVector.z));
}
else
{
Cursor.lockState = CursorLockMode.None;
}
transform.position = (Follow.transform.position + CameraPos);
}
}
到目前为止,我遇到的唯一一件事是通过 .NET 移动它,但这根本不是真正的多平台,而且似乎也是一个普遍的坏主意。
我唯一的选择是在 Vector3 位置制作一个我可以控制的 "virtual mouse" 吗?这不会与已经内置的 GUI 引擎 unity 一起工作吗?
有一个解决方法
无论何时解锁光标,您都可以将其位置设置为您想要的位置。
首先使用语句添加这些。
using System.Runtime.InteropServices;
using System.Drawing;
然后将这些行添加到您的 class。
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pos);
Point cursorPos = new Point();
那么在你的启动函数中这个
void Start()
{
GetCursorPos(out cursorPos);
}
最后你可以在任何地方调用函数 SetCursorPos(x, y);
将光标设置到屏幕上的任何点 x 和 y 是你屏幕上的坐标
我怀疑 Vector2 能否在该函数中轻松使用,但如果您弄清楚偏移量,您可能就可以使用。
基本上我想要一个点击并拖动相机,但不需要每次都将光标传送到屏幕中央。 到目前为止,我对相机移动的了解是:
public class CameraController : MonoBehaviour
{
Vector3 camRot = new Vector3(0, 0, 0);
Vector3 camPosRot = new Vector3(0, 0, 0);
public float VSpeed = 2.0f;
public float HSpeed = 2.0f;
public Vector3 CameraOffset = new Vector3(0,0,0);
public Rigidbody Follow;
public float Distance = 0.1f;
Vector3 CursorBC;
Vector3 CameraPos = new Vector3(0, 0, 0);
// Start is called before the first frame update
void Start()
{
//Follow = GetComponent<Rigidbody>();
CameraPos = CameraOffset;
}
// Update is called once per frame
void Update()
{
camRot += new Vector3(-Input.GetAxis("Mouse Y") * VSpeed, Input.GetAxis("Mouse X") * HSpeed, 0);
if (Input.GetKey(KeyCode.Mouse1))
{
CursorBC = Input.mousePosition;
Cursor.lockState = CursorLockMode.Locked;
transform.eulerAngles = camRot;
camPosRot = new Vector3(camRot.y,camRot.x);
Vector3 RotationVector = new Vector3(Mathf.Sin(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad), Mathf.Sin(camPosRot.y * Mathf.Deg2Rad), Mathf.Cos(camPosRot.x * Mathf.Deg2Rad) * Mathf.Cos(camPosRot.y * Mathf.Deg2Rad));
CameraPos = new Vector3(CameraOffset.magnitude * -(RotationVector.x), CameraOffset.magnitude * (RotationVector.y), CameraOffset.magnitude * -(RotationVector.z));
}
else
{
Cursor.lockState = CursorLockMode.None;
}
transform.position = (Follow.transform.position + CameraPos);
}
}
到目前为止,我遇到的唯一一件事是通过 .NET 移动它,但这根本不是真正的多平台,而且似乎也是一个普遍的坏主意。 我唯一的选择是在 Vector3 位置制作一个我可以控制的 "virtual mouse" 吗?这不会与已经内置的 GUI 引擎 unity 一起工作吗?
有一个解决方法
无论何时解锁光标,您都可以将其位置设置为您想要的位置。
首先使用语句添加这些。
using System.Runtime.InteropServices;
using System.Drawing;
然后将这些行添加到您的 class。
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pos);
Point cursorPos = new Point();
那么在你的启动函数中这个
void Start()
{
GetCursorPos(out cursorPos);
}
最后你可以在任何地方调用函数 SetCursorPos(x, y);
将光标设置到屏幕上的任何点 x 和 y 是你屏幕上的坐标
我怀疑 Vector2 能否在该函数中轻松使用,但如果您弄清楚偏移量,您可能就可以使用。