Unity3d Unity 2D如何设置触摸按钮?
我为android做了一个游戏,我需要在游戏屏幕上有UI操纵杆和UI攻击按钮。我已经有了可操作的操纵杆和键盘空格键上的攻击按钮。 有人能告诉我如何将代码从键盘输入切换到触摸输入吗? 另外,我为我的英语感到抱歉Unity3d Unity 2D如何设置触摸按钮?,unity3d,user-interface,Unity3d,User Interface,我为android做了一个游戏,我需要在游戏屏幕上有UI操纵杆和UI攻击按钮。我已经有了可操作的操纵杆和键盘空格键上的攻击按钮。 有人能告诉我如何将代码从键盘输入切换到触摸输入吗? 另外,我为我的英语感到抱歉 if (Time.time >= nextAttackTime) { if (Input.GetKeyDown(KeyCode.Space)) { Attack(); nextAt
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
nextAttackTime = Time.time + 2.5f / attackRate;
}
}
试试这个
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
//Make sure to attach these Buttons in the Inspector
public Button YourButton,
void Start()
{
YourButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){ /// this method will active when press the button
if (Time.time >= nextAttackTime)
{
Attack();
nextAttackTime = Time.time + 2.5f / attackRate;
}
}
}
另一种方法是使用unity按钮脚本,将其应用于表示按钮的游戏对象。在“目标图形”下,添加与图形对应的对象。在On Click()添加事件下,将具有攻击功能的游戏对象放入(无对象),并将攻击功能添加到函数下 以下是有关按钮工作原理的一些信息:
编辑:这里也有答案这是否回答了您的问题?谢谢你的支持!有一个新的错误“CS0079事件”按钮。onClick“只能出现在+=或-=”的左侧已修复它!我只是没有“使用UnityEngine.UI”,非常感谢!