Author Topic: Widgets anchored to panels occasionally position themselves using a wrong camera  (Read 2572 times)

bac9

  • Full Member
  • ***
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 113
    • View Profile
Pretty weird editor side error, not sure what can be the cause of this. It usually happens when the Editor loses focus, for example when you switch to your IDE or browser and come back later - at that point, there is a chance that UI widgets anchored to panels (for example, some top-level UIWidget holder attached to a top-right corner of it's panel) disappear from view of the UI camera and go into some extreme position like (-23223485, -6451236). This can only be fixed by reloading a scene or by pressing Apply on a prefab enclosing your UI hierarchy (that probably calls some method like OnEnable that reruns parts of UI code responsible for the issue).

I was very puzzled about the possible reasons behind this until one day I moved my main gameplay camera around an area where UI camera and elements are situated. I've left the UI layer rendered by the gameplay camera so that you'll see the situation clearly:


▲ WebM, click to view

And here is how things are supposed to work, properly anchored to UI camera:


▲ WebM, click to view

What might be causing this temporary use of a completely unrelated camera?

I'm using Unity 5.4.1f1, up to date version of NGUI. Nothing exotic about my setup, just one 2D UI in the scene, arranged into a traditional NGUI hiearchy (parent -> camera, root -> panels -> widgets).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Make sure no cameras are able to draw your UI layer except your UI camera. Are you on a Mac and is your game window hidden by any chance?

bac9

  • Full Member
  • ***
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 113
    • View Profile
So, to confirm: somewhere, NGUI code finds all Camera objects in a scene and then selects the first one with the UI layer visible, and that's the source of the issue, right?

Curious about it because while it wouldn't be an issue for my current scene, that might have implications for scenes with multiple UI hierarchies/cameras.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
That specific location is NGUITools.FindCameraForLayer -- this is the function that loops through the cameras to find a specific one that draws the UI layer, yes.

It first checks all existing UI cameras (cameras with UICamera script attached). If none are found, then it checks all Unity cameras.