Author Topic: Problems changing layer of objects.  (Read 5497 times)

roberto_sc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Problems changing layer of objects.
« on: October 29, 2013, 09:30:56 PM »
I'm working on a "split screen" mode of a game and I have some GUI elements that should appear in only one of the two cameras.

I have a quite regular NGUI hierarchy:
-UI Root
--MainCamera
---MainCameraClone
---SomePrefab
----Anchor1
----Anchor2

Except for MainCameraClone everything is like a generic NGUI tree. MainCameraClone is just a camera with the same attributes of MainCamera but with a different layer mask.
MainCamera can see layers "GUI" and "Player1" and MainCameraClone can see "GUI" and "Player2".
All objects have "GUI" as their layer, so their appear on both cameras.

I made a script to change the layer of the gameObject and all of its children, and attached it to Anchor2.
When I hit play I can see that the layer of Anchor2 and its children is different than GUI. Nevertheless the camera object appear on both camera.

It works when I add the script to SomePrefab instead of Anchor2, but I can't do that since Anchor1 must keep its original layer. And I don't want to separate the prefab in 2 because there are many prefabs.

So why do this happen? An object should be seen by the camera that can see its layer independent of the value of its parents layer, right?



roberto_sc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Problems changing layer of objects.
« Reply #1 on: October 29, 2013, 10:02:48 PM »
Did some further investigation and found out that it doesn't matter the layer of the object, all that matter is the layer of the object that contains the UIPanel script. Is that right?

So is it possible somehow to have objects with different layers under the same UIPanel?

roberto_sc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Problems changing layer of objects.
« Reply #2 on: October 29, 2013, 11:19:03 PM »
So I've seen that you move widgets to the panel's layer on LateUpdate and that UIPanel is after the default time on script execution order.
The weird thing is that I did the same thing with my script (changed layer on LateUpdate) and after yours in the script execution order and still wasn't able to see it in only one camera.
Anyways... still need your help to find out the best way to do this.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problems changing layer of objects.
« Reply #3 on: October 30, 2013, 05:23:38 AM »
Why are you changing layers at run time? Panels force-change widgets to use their layer. Think of "panel" as a renderer that sits on some layer, and "widgets" as an assortment of meshes that get drawn by the renderer. Meshes don't need layers, but the renderer does. Widgets need to use the same layer because of raycasts, however.

roberto_sc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Problems changing layer of objects.
« Reply #4 on: October 30, 2013, 05:53:10 AM »
In split screen mode, there is one camera for each of two players. Some GUI objects have to be displayed for one player, depending on whose turn is. Other GUI objects have to be displayed for both players all the time.

The problem is that split screen mode is an addition to an existing game, and this game has already many prefabs containing GUI objects for both cameras and GUI objects for one camera under the same Panel.

It will require a great amount of work to separate the prefabs and refactor the code to make them under different panels.

So is there an easier way to do this? What if I create a script that adds a new UIPanel in the "branch" that I need to have a different layer? This new UIPanel would have same attributes of its parent UIPanel but different layer.