Possible for 10 canvas in 1 scene?
A game being based on UI only, I didn't want to create 10 + different scenes when the UI stays the same throughout every single one.
Example would be: Game loads to main canvas, button leads 2nd canvas, button leads to 3rd canvas, button leads to 7th canvas, button leads to 3rd canvas, button leads to 9th canvas, button leads to 2nd canvas, button leads to 5th canvas, so on and so forth.
Answer by highpockets · Jun 02, 2019 at 11:06 PM
You can have them all on screen overlay mode and they can all be disabled except for the main canvas at the beginning. Then you can disable the one you are on and enable the one you want to switch to when you want to switch.
That or you can set the canvases to world space and move your camera around according to the different canvas positions or you can move the canvases toward the camera.
Cheers
I don't follow, screen overlay mode? Am I also able to script with that? Example, script states while button 1 is pressed have canvas 1 up, while button 2 is pressed have canvas 2 up, while button 3 is pressed have canvas 3 up?
As for the 2nd part, could it be an instantaneous move when it switches so it's not like panning over nothing?
If you want an instantaneous move, I would go with the first option. On the canvas, the component, “canvas”, has a setting called “render mode” and there are three options, choose “screen overlay mode”. Now that automatically sets all of the canvas’s to the screen size no matter their position in world space. Now you have a script with an array of canvas’s which represent those canvas’s. You manually disable each game object in the editor except the one that you want to start with. Now you just come up with a script which, like you said can have a state for each canvas. I would use an enum for your states with a public getter/setter and a private version as well. Inside the setter, you can check the value and set the canvas according to that and turn off the canvas of the canvas which the private enum represents. When a button that represents a canvas is clicked, if it is not the button that equals the current enum state, then change it the public enum to that which will internally check the value of the private enum before setting it. Hope that makes sense.
Here is some code now that I'm at my computer:
[System.Flags]
public enum CanvasState
{
CanvasOne = 0,
CanvasTwo = 1 << 0,
CanvasThree = 1 << 1,
CanvasFour = 1 << 2
}
public class CanvasControl : $$anonymous$$onoBehaviour
{
[SerializeField] Transform[] canvases; //enter the amount of canvases and place each one inside the respective fields in the inspector
private Transform currentCanvas;
private CanvasState cState = CanvasState.CanvasOne;
public CanvasState _cState
{
get { return cState; }
set
{
if(value != cState)
{
currentCanvas.gameObject.SetActive(false);
cState = value;
switch (cState)
{
case CanvasState.CanvasOne:
canvases[0].gameObject.SetActive(true);
currentCanvas = canvases[0];
case CanvasState.CanvasTwo:
canvases[1].gameObject.SetActive(true);
currentCanvas = canvases[1];
case CanvasState.CanvasThree:
canvases[2].gameObject.SetActive(true);
currentCanvas = canvases[2];
case CanvasState.CanvasFour:
canvases[3].gameObject.SetActive(true);
currentCanvas = canvases[3];
}
}
}
}
}
That is some untested code, hope that helps