Topic: Getting correct optimal zoom to fit all points within visible map area
Hey, this problem is similar to the https://forum.infinity-code.com/viewtopic.php?id=2209
I have in my app top panel, sidebar and timeline panel, which take away some space of the screen - see the attached image. The sidebar can change width.
The map takes the remaining part of the screen. I'm trying to center the map and calculate optimal zoom value to nicely see all of given points.
My map size is 2048x1536. In the tileset component size on the scene is also 2048x1536 - maybe that's a mistake? Can I change it in runtime?
My canvas is a screen space overlay canvas, with canvas scaler set to "Scale with screen size + reference resolution is 1920x1080".
How can I make it work on all canvas/screen sizes and different aspect ratios? I've noticed there is a Vector2 inset parametr in the OnlineMapsUtils.GetCenterPointAndZoom method, but I cannot make it work perfectly.
My current code:
Vector2 crop = Vector2.zero;
GameObject uiCanvas = GameObject.FindGameObjectWithTag(Global.Tags.UI_CANVAS);
GameObject sidebarGO = GameObject.FindGameObjectWithTag(Global.Tags.SIDEBAR_PANEL);
if (sidebarGO != null && uiCanvas != null)
{
SidebarController sc = sidebarGO.GetComponentInChildren<SidebarController>(true);
RectTransform canvasRT = uiCanvas.GetComponent<RectTransform>();
if (sc != null && canvasRT != null)
{
crop = new Vector2(
1.0f * (sc.expanded ? SIDEBAR_WIDTH_EXPANDED : SIDEBAR_WIDTH_COLLAPSED) / canvasRT.sizeDelta.x,
1.0f * TIMELINE_HEIGHT_COLLAPSED / canvasRT.sizeDelta.y);
crop /= canvasRT.localScale.x;
}
}
OnlineMapsUtils.GetCenterPointAndZoom(positions.ToArray(), out Vector2 center, out int zoom, crop);
OnlineMapsControlBase.instance.map.SetPositionAndZoom(center.x, center.y, zoom);How is the map positioned in the screen? Is the center of the map in the center of the screen?
Thanks for help.