UGUI之UI的深度问题

学过NGUI的都知道,NGUI的深度是通过值来控制的。Panel也是UI也是,如果空间太多,布局复杂UI深度的值会变得很混乱。所以在NGUI中设置UI深度时一定要多加思考。然而在UGUI控制显示顺序的深度不是值,而是位置。只要通过管理UI控件的位置,就可以很好的管理UI的显示顺序。

我们可以看到,当button1在下,button2在上时,scene中的button为button1靠外,button2靠里。button2的深度大于button1的深度。如果要调换他们的显示深度,同样,也只需要在Hierarchy试图中调换一下他们的位置就可以了。

button如是,button的子物体也如是。

如果我们想运行的时候在两个图之间插一个图就只需要通过transform.SetSiblingIndex 和 GetSiblingIndex。把需要插入图的GameObject在兄弟节点的位置就可以了。

 GameObject button = GameObject.Instantiate(Resources.Load("button"))as GameObject;
 button.transform.parent = transform;
 button.transform.localPosition = Vector3.zero;
 button.transform.localScale = Vector3.one;
 GameObject AObj = transform.Find("A").gameObject;
 GameObject BObj = transform.Find("B").gameObject;
 button.transform.SetSiblingIndex(AObj.transform.GetSiblingIndex());

http://www.banbaise.com/archives/533

转载请注明:半白色 » UGUI之UI的深度问题(十)

posted @ 2015-12-01 14:21  半白色  阅读(883)  评论(0编辑  收藏  举报