【unity】获取游戏对象,获取对象身上的组件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/*
作者:烧仙草奶茶
*/
namespace SC_MahJong
{
	public class check : MonoBehaviour
	{
        //创建游戏对象方法
        public Button Button;//获取游戏对象方法:1.在unity中拖动对象,获取游戏对象
        public GameObject a;
		// Start is called before the first frame update
		void Start()
		{

            /*获取游戏对象方法:*/
            //查找需要耗时很多,建议在start里先加载,在update里使用
            //1.在脚本中创建对象public GameObject a,之后在unity中拖动对象,获取游戏对象
            //2.GameObject.Find
                //通过名字或路径查找游戏对象。
            GameObject.Find("GameObject");//通过名字查找游戏对象,可以不加路径,如果有重名不方便查找
            GameObject.Find("GameObject/ChildGameObject");
            GameObject.FindWithTag("tag");//通过标签查找
            GameObject.FindGameObjectsWithTag("tag");//返回一个数组,查找含标签的所有物体
            GameObject.FindObjectOfType<Button>();//返回该类型的第一个活动对象
            GameObject.FindObjectsOfType<Image>();//返回类型为该类型的所有对象
            //3.transform.Find
            a.transform.Find("Button");//通过路径查找对象,button为当前对象子节点
            a.transform.parent.Find("Button");//parent获取父物体

            /*获取对象身上的组件*/

            a.GetComponent<Button>();//获取Button组件
            this.GetComponent<Text>();//this:当前脚本所依附的对象
            a.GetComponent<Image>();
            this.GetComponent<Text>().text = "lili14444";
            //获取对象身上的button组件,添加点击事件监听
            a.GetComponent<Button>().onClick.AddListener(()=> {
            });
		}
        

        // Update is called once per frame
        void Update()
		{
			
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值