所以在过去两天(不开玩笑)我一直在试图弄清楚如何按下按钮,然后如果是这样,让它实例化一个对象。到目前为止,我已尝试过多种方法,也许其中一种方法更接近于我想要的地方,但现在我只会展示我目前所拥有的。现在,问题在于我检测到按钮是否在第一时间被点击了。
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);
}
}
答案 0 :(得分:0)