Unity3d C#-如何检查按钮是否已按下,然后实例化对象
所以在过去的两天里(不是开玩笑的),我一直在试图弄清楚如何检查按钮是否按下,如果是的话,让它实例化一个对象。到目前为止,我已经尝试了多种方法,也许其中一种方法更接近我想要的结果,但现在我只想展示一下我目前拥有的。现在,问题是我检测按钮是否在第一时间被点击Unity3d C#-如何检查按钮是否已按下,然后实例化对象,c#,user-interface,unity3d,C#,User Interface,Unity3d,所以在过去的两天里(不是开玩笑的),我一直在试图弄清楚如何检查按钮是否按下,如果是的话,让它实例化一个对象。到目前为止,我已经尝试了多种方法,也许其中一种方法更接近我想要的结果,但现在我只想展示一下我目前拥有的。现在,问题是我检测按钮是否在第一时间被点击 using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class Master :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Master : MonoBehaviour
{
public Button storeButton;
public Transform hands;
public GameObject gun1;
void OnEnable()
{
gun1 = GameObject.FindGameObjectWithTag("Weapon1");
hands = GameObject.FindGameObjectWithTag("Player").transform;
// storeButton.onClick.AddListener(MyFunction);//adds a listener for when you click the button
storeButton.onClick.AddListener(() => MyFunction());
}
void MyFunction()
{
Debug.Log("YOUR PRESSED THE DAMN BUTTON");
// Instantiate(gun1, hands.position, hands.rotation);
GameObject.Instantiate(gun1, hands.position, hands.rotation);
}
}
将事件系统添加到场景中 否则你就不能点击其他东西
到目前为止看起来不错。控制台中有错误日志吗?可能尝试Start()方法而不是OnEnable()。没有错误,并且我的Debug.Log不会被发送。我尝试使用Start()时也遇到了同样的问题:/n您可以尝试在检查器上设置操作。您的场景中是否有EventSystem?鼠标悬停时按钮是否会播放动画?@Marcel当时我没有!现在开始工作了!谢谢