1
\$\begingroup\$

In the project I am currently working on I have two sets of main UI: one set that is on the screen at all times for the players use, and the second set which the player can open and look at to gauge their progress and see things about the game world(think pokedex) . The second set also has 3d models in front of the UI in places. To achieve this, I have a script that changes the canvas render mode to Screen Space - Camera when the 2nd UI opens, and I instantiate a 3d model in front of it. My problem is when I switch the canvas back to Screen Space - Overlay, it jumps down in the corner.

My to switch the canvas mode to Camera is as follows:

 /*--Adjust canvas--*/
     canvas.renderMode = RenderMode.OverlayCamera;
     canvas.planeDistance = canvasPlaneDistance;  //1 in this case

My code to switch the canvas back to overlay looks like this:

 /*--Adjust canvas render mode and position--*/
     canvas.renderMode = RenderMode.Overlay;

I discovered that it seems the problem is because the z pos of the Canvas's Rect Transform changes value when I initially change the render mode and this displaces it when I switch it back. I can adjust it back into position in the code by doing this:

canvas.GetComponent<RectTransform>().localPosition = 
         new Vector3(canvas.GetComponent<RectTransform>().localPosition.x,
                     canvas.GetComponent<RectTransform>().localPosition.y,
                     0);

But this causes a disorienting flicker which I would rather avoid. Is there any workaround or fix for this?

\$\endgroup\$
0
\$\begingroup\$

Instead of changing the render mode of the canvas you could instantiate the 3d model and a Camera that points at it in an unused part of the scene (or set the Camera culling settings to only render the 3d model). That camera then renders to a texture that is used by an Image element in the alternate GUI.

You'll need Unity Pro though.

\$\endgroup\$
  • \$\begingroup\$ It seems like my issue was reduced with the latest release, but using an alternative camera could have been a good approach (if we had done things that way from the start). \$\endgroup\$ – Christian Dec 1 '14 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.