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?