Unity如何在Unity中制作视觉JoyStick [英] Unity how to make a Visual JoyStick in Unity
问题描述
我正在编写一个脚本来放置一个按钮,该按钮将检测拖动方向来移动玩家
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.
推荐答案
您可以使用Unity新的事件来实现自己的可视游戏杆回调函数,例如OnBeginDrag
OnDrag
和OnEndDrag
函数.对于Unity,已经有一个现成的Visual Joystick程序包,因此实现自己的视觉效果就像重新发明轮子一样.
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.
所有您需要做的就是从Unity的CrossPlatformInputManager包. > UnityStandardAssets ,然后使用CrossPlatformInputManager.GetAxis("Horizontal")
和CrossPlatformInputManager.GetAxisRaw("Horizontal")
读取图像/拇指的方向.
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中制作视觉JoyStick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!