最近再写一个NGUI下替换所有label字体颜色的程序,需要代码获取UIroot下的所有UIlabel。但是有部分UIlabel是没有激活的,尝试了几种方式来获取。
1. GetComponentInChild方法
将代码写在start方法中,挂在UIRoot上,如下
UILabel[] childs =GetComponentsInChildren<UILabel>(true);
foreach (UILabel child in childs) {
Debug.Log (child);
}
即能获取激活状态的UILabel,又能获取未激活的UILabel
UI层次结构
运行结果输出如下,激活和未激活的label都可以找到
调试台运行结果
参考:http://answers.unity3d.com/questions/52560/gameobjectfind-work-on-inactive-objects.html