NGUI的UIPanel.cs
void UpdateLayers ()//line 1598
UIRoot的子物体会自动修改 为 UIRoot 的Layer一样的层级,在这里修改逻辑。
最好是 通过gameObject.tag进行逻辑判断。
...
public class UIPanel : UIRect
{
...
void UpdateLayers ()
{
// Always move widgets to the panel's layer
if (mLayer != cachedGameObject.layer)
{
mLayer = mGo.layer;
for (int i = 0, imax = widgets.Count; i < imax; ++i)
{
UIWidget w = widgets[i];
if (w && w.parent == this) w.gameObject.layer = mLayer;
}
ResetAnchors();
for (int i = 0; i < drawCalls.Count; ++i)
drawCalls[i].gameObject.layer = mLayer;
}
...
if(this.gameObject.Tag == "Test")
{
ObjectCommon.changAllChildLayer(this.transform, 8);//
}
...
}
...
}
如果把2D和3D放在一个 UIROOT 下面,最好是 把3DUI 的物体 添加 NGUIPlane组件。
因为修改NGUI的UIPanel.cs的UpdateLayers的逻辑,没有NGUIPlane组件,就不会执行。
public class ObjectCommon
...
public static void changAllChildLayer(Transform trans,int layer)
{
Transform[] allTrans = trans.GetComponentsInChildren<Transform>(true);
for (int i = 0; i < allTrans.Length; i++)
{
//Debug.Log("所有该脚本的物体下的子物体和孙物体名称:" + allTrans[i].gameObject.name);
Debug.Log("transformList[" + i + "]" + allTrans[i].name + "/" + allTrans[i].gameObject.layer);
allTrans[i].gameObject.layer = layer;
}
}
...
参考资料:
1.
Unity 遍历子物体与孙物体
2.