Unity3d 有没有办法将光标锁定在unity中的特定屏幕位置?
基本上我想要一个点击并拖动的相机,但不需要每次都将光标传送到屏幕中心。 到目前为止,我对摄像机运动的了解是:Unity3d 有没有办法将光标锁定在unity中的特定屏幕位置?,unity3d,camera,mouse,Unity3d,Camera,Mouse,基本上我想要一个点击并拖动的相机,但不需要每次都将光标传送到屏幕中心。 到目前为止,我对摄像机运动的了解是: 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
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);
}
}
公共类CameraController:MonoBehavior
{
Vector3 camRot=新的Vector3(0,0,0);
Vector3 camPosRot=新Vector3(0,0,0);
公共浮动速度=2.0f;
公共浮动HSpeed=2.0f;
公共矢量3摄影机偏移=新矢量3(0,0,0);
公共刚体跟随;
公共浮动距离=0.1f;
向量3-c;
Vector3摄像机=新的Vector3(0,0,0);
//在第一帧更新之前调用Start
void Start()
{
//Follow=GetComponent();
CameraPos=CameraOffset;
}
//每帧调用一次更新
无效更新()
{
camRot+=newvector3(-Input.GetAxis(“鼠标Y”)*VSpeed,Input.GetAxis(“鼠标X”)*HSpeed,0);
if(Input.GetKey(KeyCode.Mouse1))
{
CursorBC=Input.mousePosition;
Cursor.lockState=CursorLockMode.Locked;
transform.eulerAngles=camRot;
camPosRot=新矢量3(camRot.y,camRot.x);
向量3旋转向量=新向量3(数学Sin(camPosRot.x*Mathf.Deg2Rad)*数学Cos(camPosRot.y*Mathf.Deg2Rad),数学Sin(camPosRot.y*Mathf.Deg2Rad),数学Cos(camPosRot.x*Mathf.Deg2Rad)*数学Cos(camPosRot.y*Mathf Deg2Rad));
CameraPos=新矢量3(CameraOffset.magnitude*-(RotationVector.x)、CameraOffset.magnitude*(RotationVector.y)、CameraOffset.magnitude*-(RotationVector.z));
}
其他的
{
Cursor.lockState=CursorLockMode.None;
}
transform.position=(Follow.transform.position+CameraPos);
}
}
到目前为止,我遇到的唯一一件事是将其通过.NET进行移动,但这并不是真正的多平台,而且看起来是一个总体上不好的主意。
我唯一的选择是在Vector3位置制作一个“虚拟鼠标”,我可以控制它,但它不适用于unity已经内置的GUI引擎吗?有一个解决方法
无论何时解锁光标,都可以将其位置设置为所需的位置
首先使用stations添加这些
using System.Runtime.InteropServices;
using System.Drawing;
然后将这些行添加到您的类中
[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);
}
最后,您可以调用functionSetCursorPos(x,y)
anywhere可将光标设置为屏幕上的任意点x和y,即屏幕上的坐标
我怀疑Vector2在函数中是否可用,但如果您计算出偏移量,您可能可以。如果我能从unity获得准确的屏幕位置,可能会有用,但我真的希望避免这种情况,因为它看起来并不是多平台的。。。我猜o可以找到如何为我想要的每一个平台做这件事……当然,每个目标平台都有这样做的方法,但这需要一些研究,我希望我指导你正确的方向,并给我一个甜美的绿色勾号:)谢谢。我想看看是否有现成的工具可以做到这一点多平台如果不是,我会给你一个Cookie好的,这会起作用,但它不是多平台的。如果有人在linux和osx上发现了相同的东西,请确保添加它们。我在这里找到了linux上的一点线索,而在osx上我没有发现任何线索,但在我看来,您必须显著更改您的linux/osx构建,因此基本上您必须维护三个项目。我现在明白了为什么大制片厂永远不会在这些平台上发行,谢谢你的cookie:)