- Home /
Getting the Camera to Match the Size of the canvas with dynamically added UI elements
What I am trying to do is get the camera to show all of the UI elements being generated, even if that means they are resized or there is a black area to the left or right of main content. Basically just trying to generate a tilemap for testing purposes right now. And yes, the length and width in tiles of the map can change. How would I go about doing this without changing the UI elements' position in relation to each other? When I have the Canvas Scaler Scale mode Set to Constant Pixel Size, I get the look I want from the elements, but they are not within the camera viewport. If I change the Scale mode to Scale with Screen Size I still don't get all the elements in the viewport and now they are much smaller and more spread out in relation to each other. I'll attach some images of my canvas settings. Thanks in advance!
Oh and here is the code I am using to instantiate the tiles:
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
GameObject.Instantiate(emptyTile, new Vector3(i * tileSize, j * tileSize), Quaternion.identity, alt textGameObject.FindObjectOfType<Canvas>().transform);
}
}
Answer by Bamboy · Jun 20, 2017 at 01:35 PM
I'm having a similar problem to this. I want to create a minimap where the small camera doing the rendering matches the size of a rect transform under the canvas so that I can have a clean border. Like the OP said, setting the canvas scaler mode to constant pixel size gets the result I want, but then my other UI isn't gonna scale correctly for different resolutions. Doing a seperate canvas doesn't exactly work either as the minimap cam is likely to overlap my other UI.
This seems like it should be easy to do, but I can't seem to figure out the math or whatever is required. Help would be appreciated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to adjust collision detection according to URP viewport rectangle change 0 Answers
How to adjust collision detection according to URP viewport rectangle change 0 Answers
Clamp To The Screen Edge 0 Answers
What to use for dynamic loading of assets instead of Resources.Load()? 1 Answer