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

UGUI之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的深度问题(十)

相关文章:

  • 2021-06-09
  • 2022-12-23
  • 2021-12-03
  • 2021-04-29
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2021-10-19
  • 2022-12-23
  • 2021-11-30
  • 2021-12-30
  • 2022-12-23
  • 2021-06-02
相关资源
相似解决方案