VR环形、弧形菜单在开发中经常用到,当处于视野一边时,前面的元素显示正常,但是处于另一边时显示则是透明的。这是由于Unity对场景对象渲染机制是采用由上到下,由外到里的顺序。所以当我们的UI做好了后,下面子元素的顺序就确定了,导致以上现象的发生。
避免这种现象的方法也很简单,就是先判断UI菜单中心位置是位于玩家的左边还是右边,判断完毕再根据具体情况来确定子对象的顺序。下面上代码:
/// <summary>
/// 改变指定游戏对象下面的子对象顺序
/// </summary>
/// <param name="Objs">指定对象的子对象数组</param>
public static void ChangeChildrenOrder(GameObject[] Objs)
{
if (Objs != null && Objs.Length > 0)
{
for (int i = 0; i < Objs.Length; i++)
{
Objs[i].transform.SetSiblingIndex(Objs.Length - 1 - i);
}
}
}
之所以用静态方法,是因为我把这个方法作为了一个公用的工具方法。