- Home /
Adjust overlay position for different resolutions
I use a sprite as background with the anchors set to stretch vertically and center horizontally: There are other sprite overlays I manage in code with different size than the background. Such an overlay should always be positioned at a specific location in relation to the background image and scale properly. They should produce the same result I would get when the background and overlay would be just one image with the size of the background. I haven't been able to figure out how to do this best. To visualize what I intend to do:
Left bottom is the background with the overlay and I know its position and size for the unscaled background. I can set the values for the overlay with overlay.rectTransform.sizeDelta and overlay.rectTransform.position. The problem is the bottom right side which is a different game resolution so the background height got scaled down and less of the left and right borders is visible. I don't know how to set the anchors or calculate the position and scaling for the overlay to get it positioned correctly in relation to the background and new resolution.
you didn't say how it should look on a different aspect than the one you designed it for. btw. you shouldn't need to code a single line if the layouting was done right.
It should always look like the background and overlay are one image. So it should behave for every situation like it would when I design the background with 1920x1080 inside a graphic program and add the overly directly there to save both as one background image ins$$anonymous$$d of two images. The merged background image would get then the anchor settings mentioned at the start.
I have multiple different sized overlays which change later in the game so my assumption was I will probably have to adjust it every time in code when the overlay changes.
There are several ways to position something in relation to something else. Should the overlay orient itself to the backgrounds upper left corner? Or the side? $$anonymous$$aybe the center? Is the downscale of the background in the vertical direction, and the cut off in the horizontal direction the way you want the background to behave? 16:9 just isn't the same as 4:3 or any other aspect, so how exactly do you want it to look like, not thinking of it in absolute numbers but relative information like "10% of the total width from left", or "always 50 pixels in from the upper left corner in both directions"
Your answer
Follow this Question
Related Questions
Problems assigning sprite's images 1 Answer
doubt with pixels per unit 1 Answer
How to realize Image.fillAmount with Sprites in game? 0 Answers
Photoshop drawings loose quality in Unity? 1 Answer
Handling Multiple Resolutions in 2D? 0 Answers