首页 > 技术文章 > Unity3D:UGUI遍历子控件

makebetter 2017-04-13 15:25 原文

参考文章:http://blog.csdn.net/u011185231/article/details/49591383

todo:active和enable的差别?

核心API:

Component.GetComponentInChildren<Transform>(),应该是递归获取所有的child,激活的

GetComponentsInChildren<Transform>(true),激活和未激活的都get

思路:获取Transform,然后在获取上面的GameObject。然后对child进行操作。

		//img_bottomPanel_loading是Image
		//GetComponentInChildren获取第一个child
		Transform firstChild = img_bottomPanel_loading.GetComponentInChildren<Transform> ();
		string name = firstChild.gameObject.name;
		Debug.Log ("firstChild:" + name);

		//获取所有的children,
		Transform[] children = img_bottomPanel_loading.GetComponentsInChildren<Transform> ();
		for (int i = 0; i < children.Length; i++) {
			string nameTemp = children [i].gameObject.name;
			children [i].gameObject.SetActive (false); //hide
			Debug.Log ("nameTemp:" + nameTemp);
		}
	

 

	private void setBottomOperatePanelHide(bool hide){
		img_bottomPanel_operate.enabled = !hide;
		Transform[] children = img_bottomPanel_operate.GetComponentsInChildren<Transform> (true);
		foreach(Transform tr in children){
			tr.gameObject.SetActive (hide);
		}
	}

  

推荐阅读