Unity 如何在 Unity 中制作可视化操纵杆 [英] Unity how to make a Visual JoyStick in Unity

查看:35
本文介绍了Unity 如何在 Unity 中制作可视化操纵杆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

void OnGUI(){如果(buttonRect.Contains(Event.current.mousePosition)){if (Event.current.type == EventType.MouseDown){按钮按下 = 真;}if (Event.current.type == EventType.MouseUp){按钮按下 = 假;}}if (buttonPressed && Event.current.type == EventType.MouseDrag){}}

如果将此脚本放置在按钮上,如何将按钮边界设为矩形?

此外,如果有人有更好的通过拖动控制运动的解决方案,我会很乐意提供建议.

解决方案

您可以使用 Unity 新的 事件来实现您自己的 Visual Joystick 回调函数,例如OnBeginDrag OnDragOnEndDrag 函数.Unity 已经有一个 Visual Joystick 包,所以实现你自己的就像重新发明轮子一样.

您所要做的就是从 Unity 的 CrossPlatformInputManager 包="nofollow noreferrer">UnityStandardAssets 然后使用 CrossPlatformInputManager.GetAxis("Horizo​​ntal")CrossPlatformInputManager.GetAxisRaw("Horizo​​ntal") 读取方向图片/拇指.

要从头开始制作,您可以观看这个视频教程.

I'm writing a script to put on a button that will detect a drag direction to move a player

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)
    {

    }
}

If this script was to be placed on a button, how could I get the buttons bounds as a rectangle?

Also, If anyone has a better solution for controling movement by drag I would be open for suggestions.

解决方案

You can implement your own Visual Joystick by using the Unity new event callback functions such as OnBeginDrag OnDrag and the OnEndDrag function. There is a already made Visual Joystick package out there for Unity, so implementing your own is like reinventing the wheel.

All you have to do is to import the CrossPlatformInputManager package from Unity's UnityStandardAssets then use CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal") to read the direction of the image/thumb.

To make one from scratch, you can flow this video tutorial.

这篇关于Unity 如何在 Unity 中制作可视化操纵杆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆