Unity3D Button组件及修改Button显示的文字 |
您所在的位置:网站首页 › ih5点击按钮出现文本 › Unity3D Button组件及修改Button显示的文字 |
在创建了一个Button后,结构如图: 先仔细观察一下Button的Inspector视图: 发现其中竟然有一个叫Button的脚本组件. 新建脚本,代码如下,并将该脚本绑定给Canvas组件: using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine; public class btn1 : MonoBehaviour { // Start is called before the first frame update public Button btn; void Start() { } // Update is called once per frame void Update() { GameObject go = GameObject.Find("Butt"); Text text=go.GetComponentInChildren(); text.text="天桑在玩CSGO"; Debug.Log(text.text); } }1.首先通过GameObject.Find()找到名字为Butt的游戏物体. 2.通过GetComponentInChildren()获得子目录下类型为T的组件. (这里我之前用GetComponent试过不行,是因为Button的Text在子目录下) 3.我们还可以获得Button组件: GameObject go = GameObject.Find("Butt"); Button button=go.GetComponent(); if(button)Debug.Log("找到这个按钮了!"); 这两段代码合并后的输出结果为: 这表明这两个组件都找到了. 这样一看,Button的结构就很清楚了,Button在第一级目录上,附带生成的Text在子目录上. 实验结果符合预期: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |