简体   繁体   English

从Unity中的脚本获取对按钮的引用

[英]Get a reference to a button from script in Unity

The script is attached to a prefab that is not in a scene. 该脚本附加到不在场景中的预制件。 The button has its tag. 按钮有标签。

I tried drag and dropping the button in the inspector, but the engine won't let me. 我试着在检查器中拖放按钮,但引擎不会让我。 I tried finding it by tag, but I get an exception "Cannot implicitly convert type UnityEngine.GameObject to UnityEngine.UI.Button " and when I cast I get an exception that I cannot convert these types via built-in conversion. 我尝试通过标签找到它,但我得到一个异常“不能隐式转换类型UnityEngine.GameObject到UnityEngine.UI.Button”,当我转换时,我得到一个异常,我无法通过内置转换转换这些类型。 Some help? 一些帮助? How do I get a reference to a button? 如何获得对按钮的引用? Here is the code : 这是代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TankShooting : MonoBehaviour {

    private Transform ShootingCameraTransform;
    private PlayerTankMovement playerTankMovement;
    public Button shootButton;


    // Use this for initialization
    void Start () {

        shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
        shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);

        playerTankMovement = GetComponent<PlayerTankMovement> ();
        Transform t = transform;
        foreach (Transform tr in t)
        {
            if (tr.tag == "ShootingCamera") 
            {
                ShootingCameraTransform = tr.transform;
            }
        }
    }

    // Update is called once per frame
    void Update () {

    }

    public void OnShootButtonClick()
    {
        Debug.Log ("Success");

    }
}

Looking at your code, there is another problem: 查看您的代码,还有另一个问题:

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;

You cannot cast Component( Button ) to GameObject like that. 你不能像这样将组件( Button )强制转换为GameObject。 You have to use GetComponent to get the Button component from the GameObject. 您必须使用GetComponentGetComponent获取Button组件。

I cannot drag and drop the button in the inspector and when I find it by tag I cannot cast it from GameObject to Button 我无法在检查器中拖放按钮,当我通过标签找到它时,我无法将其从GameObject转换为Button

That's because the the GameObject you are dragging to the shootButton slot is not a Button or does not have the Button component attached to it. 那是因为您拖动到shootButton插槽的GameObject不是Button或者没有附加Button组件。 It must have the Button component for you to be able to drag it to the Button (shootButton ) slot. 必须有Button组件才能将其拖动到Button (shootButton)插槽。

You have to create a Button then drag that Button to the shootButton slot. 您必须创建一个Button然后将该Button拖动到shootButton插槽。

You can do that by first removing 你可以先删除

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);

then drag the Button to the shootButton slot: 然后将Button拖到shootButton插槽:

在此输入图像描述

OR 要么

get the reference from script: 从脚本中获取引用:

If you want to do this from script, replace 如果要从脚本执行此操作,请替换

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);

with

shootButton = GameObject.FindGameObjectWithTag("ShootButton").GetComponent<Button>();
shootButton.onClick.AddListener(() => OnShootButtonClick());

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

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