它远非完美,但这里是您可以使用旧输入系统做什么的起点。
using UnityEngine;
using UnityEngine.EventSystems;
public class TestScript : StandaloneInputModule
{
[SerializeField] private KeyCode left, right, up, down, click;
[SerializeField] private RectTransform fakeCursor = null;
private float moveSpeed = 5f;
public void ClickAt(Vector2 pos, bool pressed)
{
Input.simulateMouseWithTouches = true;
var pointerData = GetTouchPointerEventData(new Touch()
{
position = pos,
}, out bool b, out bool bb);
ProcessTouchPress(pointerData, pressed, !pressed);
}
void Update()
{
// instead of the specific input checks, you can use Input.GetAxis("Horizontal") and Input.GetAxis("Vertical")
if (Input.GetKey(left))
{
fakeCursor.anchoredPosition += new Vector2(-1 * moveSpeed, 0f);
}
if (Input.GetKey(right))
{
fakeCursor.anchoredPosition += new Vector2(moveSpeed, 0f);
}
if (Input.GetKey(down))
{
fakeCursor.anchoredPosition += new Vector2(0f, -1 * moveSpeed);
}
if (Input.GetKey(up))
{
fakeCursor.anchoredPosition += new Vector2(0f, moveSpeed);
}
if (Input.GetKeyDown(click))
{
ClickAt(fakeCursor.position, true);
}
if (Input.GetKeyUp(click))
{
ClickAt(fakeCursor.position, false);
}
}
}
将KeyCode 值设置为您喜欢的任何值。在我的示例中,我将 UI 图像设置为光标并将画布渲染器设置为 Overlay,因此坐标已经在屏幕空间中。我用这个脚本替换了场景中的InputModule EventSystem。
这是脚本的 gif:
我正在使用wasd 在屏幕上移动我的假光标,当我点击space 时,它会模拟假光标位置上的点击事件。