unity获取游戏对象

RectTransform tr = this.GetComponent<RectTransform>();
RectTransform rect = (this.transform as RectTransform);

//直接获得对象身上的其它组件(类)
Button bb = this.GetComponent<Button>();// ("Panel/center/left/btn_addRoom");
Image m = this.transform.GetComponent<Image>();

//获得当前游戏对象下面的其它子节点身上的组件,先找
//用多行表示
Transform ttr2 = this.transform;
Transform ttr3 = ttr2.Find("Panel/center/left/btn_addRoom");
Button bb0 = ttr3.GetComponent<Button>();
bb0.onClick.AddListener(()=> {

});
//简单表示
Transform trr = this.transform.Find("Panel/center/left/btn_addRoom");
Button bb2 = trr.GetComponent<Button>();
bb2.onClick.AddListener(() => {

});
//改成一句话表示
this.transform.Find("Panel/center/left/btn_addRoom").GetComponent<Button>().onClick.AddListener(()=> {

});
Test t = this.transform.Find("Panel/center/left/btn_addRoom").GetComponent<Test>();
t.id = 100000;
t.tName = "lili";
t.run();

//通过this获得游戏对象本身
GameObject obj = this.gameObject;
obj.SetActive(true);//设置当前游戏对象为活动的(可见的)
obj.SetActive(false);//设置为禁用的,(不可见的);

//this.transform.Find("Panel/center/left/btn_addRoom").GetComponent<Button>();
this.gameObject.transform.Find("Panel/center/left/btn_addRoom").GetComponent<Button>();


//获得当前对象身上的组件的几种 方法
Test t1 = this.GetComponent<Test>();
Test t2 = this.transform.GetComponent<Test>();
this.gameObject.GetComponent<Test>();
this.gameObject.transform.GetComponent<Test>();


this,//代表当前类的对象,或所挂的游戏对象(只能获得所挂游戏对象的一部方法或属性)
this.transform //获得当前所有挂游戏对象的Tansform类
this.gameObject;//获得当前游戏对象本身,->GameObject游戏对象
--------------------- 

posted @ 2019-08-10 19:17  李艳艳665  阅读(489)  评论(0编辑  收藏  举报