统一将鼠标指针向特定方向移动
Move the mouse pointer in a specific direction with unity
我想用类似这样的方式移动鼠标指针:mouseposition += new Vector2(x, y)。
"GetCursorPos" 始终是 (0, 0),所以我不能用 delta 值移动鼠标指针,但我可以用 SetCursorPos 将它移动到某个坐标。我尝试了以下代码:
using UnityEngine;
using System.Runtime.InteropServices;
public class all : MonoBehaviour
{
public Vector2 MoveMouse;
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Vector2 pos);
private void FixedUpdate()
{
Vector2 cursorPos = new Vector2();
GetCursorPos(out cursorPos);
//finding the cursor's position
SetCursorPos(Mathf.RoundToInt(cursorPos.x + MoveMouse.x), Mathf.RoundToInt(cursorPos.y + MoveMouse.y));
//cursorpos += MoveMouse
}
}
我尝试了另一个代码,将 GetCursorPos 替换为另一个值,但是当我移动鼠标指针时鼠标指针会随机移动一点。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class all : MonoBehaviour
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
public Vector2 offset;
bool started;
private void Start()
{
StartCoroutine(waiting());
}
private void FixedUpdate()
{
if (started)
{
SetCursorPos(Mathf.RoundToInt(Input.mousePosition.x + offset.x), Mathf.RoundToInt(-(Input.mousePosition.y + offset.y)));
}
}
IEnumerator waiting()
{
Application.runInBackground = true;
SetCursorPos(0, 0);
yield return new WaitForEndOfFrame();
offset = new Vector2(-Input.mousePosition.x, -Input.mousePosition.y);
started = true;
}
}
所以我不得不自己做
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class MouseControl : MonoBehaviour
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePosition lpMousePosition);
public int xdmp;
public int ydmp;
[StructLayout(LayoutKind.Sequential)]
public struct MousePosition
{
public int x;
public int y;
}
private void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.Space))
{
xdmp = 0;
ydmp = 0;
}
MousePosition mp;
GetCursorPos(out mp);
print(mp.x + " " + mp.y);
SetCursorPos(mp.x + xdmp, mp.y + ydmp);
}
}
使用新的输入系统(通过 packagemanager 安装)您可以使用
Mouse.current.WarpCursorPosition(...)(截至 2020 年 11 月的输入系统版本 1.0.0,或者 1.1.0 预览版 2)。
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Mouse.html:
在桌面平台(Windows、Mac、Linux 和 UWP)上,您可以通过代码移动鼠标光标。请注意,这会移动系统的实际鼠标光标,而不仅仅是 Unity 内部存储的鼠标位置。这意味着用户看到光标跳到不同的位置,这通常被认为是糟糕的用户体验实践。建议仅在光标隐藏时执行此操作(有关更多信息,请参阅光标 API 文档)。
我想用类似这样的方式移动鼠标指针:mouseposition += new Vector2(x, y)。 "GetCursorPos" 始终是 (0, 0),所以我不能用 delta 值移动鼠标指针,但我可以用 SetCursorPos 将它移动到某个坐标。我尝试了以下代码:
using UnityEngine;
using System.Runtime.InteropServices;
public class all : MonoBehaviour
{
public Vector2 MoveMouse;
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Vector2 pos);
private void FixedUpdate()
{
Vector2 cursorPos = new Vector2();
GetCursorPos(out cursorPos);
//finding the cursor's position
SetCursorPos(Mathf.RoundToInt(cursorPos.x + MoveMouse.x), Mathf.RoundToInt(cursorPos.y + MoveMouse.y));
//cursorpos += MoveMouse
}
}
我尝试了另一个代码,将 GetCursorPos 替换为另一个值,但是当我移动鼠标指针时鼠标指针会随机移动一点。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class all : MonoBehaviour
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
public Vector2 offset;
bool started;
private void Start()
{
StartCoroutine(waiting());
}
private void FixedUpdate()
{
if (started)
{
SetCursorPos(Mathf.RoundToInt(Input.mousePosition.x + offset.x), Mathf.RoundToInt(-(Input.mousePosition.y + offset.y)));
}
}
IEnumerator waiting()
{
Application.runInBackground = true;
SetCursorPos(0, 0);
yield return new WaitForEndOfFrame();
offset = new Vector2(-Input.mousePosition.x, -Input.mousePosition.y);
started = true;
}
}
所以我不得不自己做
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class MouseControl : MonoBehaviour
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePosition lpMousePosition);
public int xdmp;
public int ydmp;
[StructLayout(LayoutKind.Sequential)]
public struct MousePosition
{
public int x;
public int y;
}
private void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.Space))
{
xdmp = 0;
ydmp = 0;
}
MousePosition mp;
GetCursorPos(out mp);
print(mp.x + " " + mp.y);
SetCursorPos(mp.x + xdmp, mp.y + ydmp);
}
}
使用新的输入系统(通过 packagemanager 安装)您可以使用 Mouse.current.WarpCursorPosition(...)(截至 2020 年 11 月的输入系统版本 1.0.0,或者 1.1.0 预览版 2)。
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Mouse.html:
在桌面平台(Windows、Mac、Linux 和 UWP)上,您可以通过代码移动鼠标光标。请注意,这会移动系统的实际鼠标光标,而不仅仅是 Unity 内部存储的鼠标位置。这意味着用户看到光标跳到不同的位置,这通常被认为是糟糕的用户体验实践。建议仅在光标隐藏时执行此操作(有关更多信息,请参阅光标 API 文档)。