繁体   English   中英

Unity3d C#-如何检查按钮是否被按下,然后实例化对象

[英]Unity3d C# - How to check if a button has been pressed, and then instantiate an object

因此,在过去的两天里(没有开玩笑),我一直在尝试找出如何检查按钮是否被按下,然后如果要按下,则使其实例化一个对象。 到目前为止,我已经尝试了多种方法,也许其中一种方法更接近我想要的位置,但是现在我将仅展示我目前拥有的方法。 现在,问题是我要检测按钮是否首先被单击。

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);
    }
}

EventSystem添加到场景中。

否则,您不能单击“其他按钮”。

http://docs.unity3d.com/Manual/EventSystem.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM