Hello everyone,
I am trying to make a UI that is above all of other UI, for both Overlay or World space UIs. So, I get GetWorldCorners(), then, if the source is a World UI, I correct this by using camera WorldToScreenPoint() transformation matrix. Then I scale the RectTransforms in the Upper UI according to these corners.
In Mobile, this have given me what I wanted, in PC, if any resolution other than 1980 x 1080, which is the reference resolution of Canvases in the game, the results aren’t consistent.
The same happens when using Aspect ratio for the “Game” tap in Unity, but if I use a set resolution, instead of a ratio, the results are exactly what I want, same as in mobile.
Correct Scaling
Wrong Scaling
Correct Scaling
Wrong Scaling
Finally, this is the code I use for scalling:
private void LateUpdate ()
{
if (refRectTransform == null)
return;
//var cornerPoints = new Vector3[4];
refRectTransform.GetWorldCorners(cornerPoints);
minCorner = (refUsesWorldSpace)? (Vector3)RectTransformUtility.WorldToScreenPoint(levelCamera, cornerPoints[0]) : cornerPoints[0];
maxCorner = (refUsesWorldSpace)? (Vector3)RectTransformUtility.WorldToScreenPoint(levelCamera, cornerPoints[2]) : cornerPoints[2];
newPos = (minCorner + maxCorner) / 2 + (Vector3)positionOffset;
newPos.z = 0;
rectTransform.position = newPos;
rectTransform.sizeDelta = new Vector2((maxCorner.x - minCorner.x), (maxCorner.y - minCorner.y)) + addSize;
}
Thank you for your time