Unity批量创建按钮

来源:互联网 发布:官方淘宝网下载安装 编辑:程序博客网 时间:2024/06/10 18:47


1.调用其他物体脚本的方法:

GameObject.Find("脚本所在物体名").GetComponent<脚本名>().函数名();

2.批量创建按钮
public class CreateRecentCourseBtn : MonoBehaviour {private string[] subjects = {"牛顿运动定律", "材料物理", "食品化学", "基础医学"};public Button BtnRecentCoursePrefab;// Use this for initializationvoid Start () {for(int i = 0; i < subjects.Length; i++){Button obj = Instantiate(BtnRecentCoursePrefab);obj.transform.SetParent(transform);obj.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0,0,0);//不设置的话可能跑到其他地方去了obj.GetComponent<RectTransform>().localScale = new Vector3(1,1,1);//不设置的话变大了Text btnText =obj.GetComponentInChildren<Text>();btnText.text = "<color=#FFFFFF>"+subjects[i]+"</color>";//改变按钮文件颜色}}// Update is called once per framevoid Update () {}}


0 0
原创粉丝点击