【问题标题】:How to move mouse cursor with joystick?如何用操纵杆移动鼠标光标?
【发布时间】:2019-07-23 11:40:53
【问题描述】:

这个问题好像已经讨论过很多次了。但我找不到适合我的情况的解决方案。所有讨论都是关于根据鼠标移动或通过鼠标控制对象的相机旋转。但我需要用操纵杆(或键盘)控制鼠标指针。这看起来是一个非常简单的问题,但我很困惑。目前,我正在尝试隐藏硬件光标并重新创建自己的光标,用Input.GetAxis("Horizontal")Input.GetAxis("Mouse X") 控制它,但光标只是消失了,事件系统报告没有移动。但是,如果我使用 Input.mousePosition.x 我的自定义光标可以很好地创建并使用鼠标进行控制。不幸的是,我只需要使用操纵杆来控制它。此外,它不适用于项目设置>>输入(或者我可能在那里更改了一些错误)提前谢谢。

public class cursor : MonoBehaviour
{
    public Texture2D cursorImage;

    private int cursorWidth = 32;
    private int cursorHeight = 32;
    public float horizontalSpeed = 2.0F;
    public float verticalSpeed = 2.0F;

    void Start()
    {
        Cursor.visible = false;
    }

    void OnGUI()
    {
        float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
        float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
        GUI.DrawTexture(new Rect(h, Screen.height - v, cursorWidth, cursorHeight), cursorImage);
    }
}

【问题讨论】:

  • 如果有一个带有图像的画布覆盖层呢?您可以使用 RectTransform 使其移动
  • @Jichael 感谢您的建议。这确实是一个解决方案,我正在考虑这个问题。但是整个应用程序功能都是关于鼠标位置(显示坐标)的,我真的不想重写所有内容并获取这个画布世界位置并将其转换为显示坐标。项目已经准备好了,我只需要实现摇杆移动即可。
  • 你知道Input.GetMouseButtonDown(0); 在这里绝对什么都不做,对吧?它返回一个你从不存储或使用的布尔值......如果你真的想实现自己的光标,可以使用 UI 和 IPointerEnterHandlerIPointerClickHandler 等,你必须实现自己的 PointerInputModule
  • @derHugo 我知道,这些行在这里没用,只是剩菜。抱歉,我会删除它们

标签: c# unity3d input mouse joystick


【解决方案1】:

你正在使用

float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
GUI.DrawTexture(new Rect(h, Screen.height - v, cursorWidth, cursorHeight), cursorImage);

hv 将始终具有非常小的值,因为乘以 Time.deltaTimeInput.GetAxis afaik 通常返回介于 01 之间的值。

这些值并不代表实际的光标位置,而是相对于最后一帧的位置变化。

您的光标将停留在左上角的某处。


相反,您应该存储当前位置并添加您的值作为对它的更改

private Vector2 cursorPosition;

private void Start()
{
    Cursor.visible = false;

    // optional place it in the center on start
    cursorPosition = new Vector2(Screen.width/2f, Screen.height/2f);
}

private void OnGUI()
{
    // these are not actual positions but the change between last frame and now
    float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;

    // add the changes to the actual cursor position
    cursorPosition.x += h;
    cursorPosition.y += v;

    GUI.DrawTexture(new Rect(cursorPosition.x, Screen.height - cursorPosition.y, cursorWidth, cursorHeight), cursorImage);
}

另请注意,horizontalSpeedverticalSpeed 位于 Pixels / Seconds 中,您可能希望一些值大于 2 ;)

我使用了200UpDownLeftRight 键。

【讨论】:

  • 太棒了,@derHugo!一如既往地信息丰富且乐于助人。非常感谢!
  • 再次打扰您深表歉意,但我还有一个问题。一切都很完美,我得到了我需要的东西,但是有一个问题,也许你可以再次帮助我。我有一个多显示器设置,光标没有转到第二个显示器。你知道什么会导致这个问题吗?谢谢。
  • 嗯老实说,我对 Unity 中的多显示器的经验为零。我唯一的猜测是 OnGUI 可能不支持多显示器 .. 你可以尝试改用普通的 Image 组件来自 Unity UI systemUI and multiple displays 也曾经存在过一个问题,因此 UI 被严格限制在主显示屏上......也许 OnGUI 仍然如此......
  • 谢谢。会尝试找出一些东西。无论如何,我总是有一个解决方案来简单地构建一个无边框窗口并将其拖到所有显示器上。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多