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()
{
}
}
}
【unity】获取游戏对象,获取对象身上的组件
最新推荐文章于 2024-05-31 21:46:32 发布