c# - Unity如何在Unity中制作一个Visual JoyStick

标签 c# unity3d

我正在编写一个脚本来放置一个按钮,该按钮将检测拖动方向以移动玩家

void OnGUI()
{
    if (buttonRect.Contains(Event.current.mousePosition))
    {
        if (Event.current.type == EventType.MouseDown)
        {
            buttonPressed = true;
        }
        if (Event.current.type == EventType.MouseUp)
        {
            buttonPressed = false;
        }
    }
    if (buttonPressed && Event.current.type == EventType.MouseDrag)
    {

    }
}

如果这个脚本要放在一个按钮上,我怎样才能让按钮的边界成为一个矩形?

此外,如果有人有更好的解决方案来通过拖动来控制移动,我会欢迎您提出建议。

最佳答案

您可以使用 Unity new event 实现您自己的视觉操纵杆回调函数,例如 OnBeginDrag OnDragOnEndDrag 函数。已经有一个适用于 Unity 的 Visual Joystick 包,所以实现你自己的包就像重新发明轮子。

您所要做的就是从 Unity 的 UnityStandardAssets 中导入 CrossPlatformInputManager 包。然后使用 CrossPlatformInputManager.GetAxis("Horizo​​ntal")CrossPlatformInputManager.GetAxisRaw("Horizo​​ntal") 读取图像/拇指的方向。

要从头开始,你可以流this视频教程。

关于c# - Unity如何在Unity中制作一个Visual JoyStick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625384/

相关文章:

c# - 无法加载文件或程序集 'Microsoft.SqlServer.Types, Version=10.0.0.0' 或其依赖项之一

c# - 在 WinForms 中遍历 CheckedListBox?

c# - 如何优化并使其更具可读性

c# - 将对象放置在预扫描房间的设定位置

c# - Unity 5 C# - 如何更改场景并从最后一个场景导入所有资源

c# - unity “Can not play a disabled audio source”

c# - SceneManager.LoadScene 在 Unity 4.6 中不起作用?

c# - BoxCollider2D bounds.Contain(somePoint) 没有按预期工作

c# - 关闭 RabbitMQ 连接,即使应用程序没有正常结束

c# - 连接列在数据表中添加数据