从Unity中的脚本获取对按钮的引用 [英] Get a reference to a button from script in Unity

查看:359
本文介绍了从Unity中的脚本获取对按钮的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本被附加到不在场景中的预制件上.该按钮具有其标签.

The script is attached to a prefab that is not in a scene. The button has its tag.

我尝试将按钮拖放到检查器中,但是引擎不允许我这样做.我尝试通过标签找到它,但出现异常无法将类型UnityEngine.GameObject隐式转换为UnityEngine.UI.Button",并且在投射时出现异常,无法通过内置转换转换这些类型. 一些帮助? 如何获得对按钮的引用? 这是代码:

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. 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;

您不能像这样将Component(Button)投射到GameObject.您必须使用GetComponent从GameObject中获取Button组件.

You cannot cast Component(Button) to GameObject like that. You have to use GetComponent to get the Button component from the GameObject.

我无法将按钮拖放到检查器中以及找到它时 通过标签,我无法将其从GameObject投射到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

这是因为要拖动到ShootButton插槽的GameObject不是Button,或者没有附加Button组件.它必须具有Button组件,您才能将其拖动到Button(shootButton)插槽.

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. It must have the Button component for you to be able to drag it to the Button(shootButton ) slot.

您必须创建一个按钮,然后将该按钮拖到shootButton插槽.

You have to create a Button then drag that Button to the shootButton slot.

您可以先删除

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

然后将Button拖动到shootButton插槽:

OR

从脚本中获取引用:

如果要通过脚本执行此操作,请替换

If you want to do this from script, replace

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

使用

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

这篇关于从Unity中的脚本获取对按钮的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆