Fit Unity UI into Forced Aspect Ratio
When you're using a forced aspect ratio, AC automatically fits legacy Unity GUI menus to the adjusted screen bounds. For Unity UI you can use this script:
To use it, create a Unity UI Canvas with a Panel that stretches over the entire screen (Rect Transform: Stretch in all directions, Left/Top/Right/Bottom all zero) and add the script to it. Then add the actual menu elements as children of this panel.
Comments
if (mainPanel == null) {
Debug.LogError("Assign Main Panel", this);
return;
}
var rect = AC.KickStarter.mainCamera.LimitMenuToAspect(new Rect(0, 0, Screen.width, Screen.height));
var scale = (m_canvasScaler != null) ? m_canvasScaler.referenceResolution.x / Screen.width : 1;
mainPanel.sizeDelta = new Vector2(scale * -2 * rect.x, scale * -2 * rect.y);
}
Hi there!
May I ask, using Position Type Manual (subtitles), with a Unity uGUI interface, with "always fit on screen" enabled and the rectTransform boundary properly set, When and how should I call Menu.SetCentre?
Ahum, I found this:
https://adventurecreator.org/tutorials/creating-inventory-bar-unity-ui
and this:
https://adventurecreator.org/forum/discussion/6893/force-aspect-ratio-and-ui-menu
and this:
https://adventurecreator.org/scripting-guide/class_a_c_1_1_menu.html#a5b24df3e8ab4e098a64eda7acf7388b8
and maybe this (2017, not sure if it's still valid):
https://adventurecreator.org/forum/discussion/6058/problem-with-force-aspect-ratio-and-ui
Just wondering,is it necessary to call SetCentre on each update? Is it expensive?
What about calling it as soon as the display size changes, or when the UI gets shows, or, (in my case) whenever a new Subtitle appears/changes?
SetCentre becomes more expensive if Always fit within screen? is checked, as it has to account for that. But if you're using Unity UI, you can bypass this and update the RectTransform component(s) directly too.
If it's stationary, a simple call when it's turned on (via the OnMenuTurnOn event) should be enough:
I'm not really sure about what to pass to menuPosition; I'm forcing a 2:1 aspect ratio and the menù is set to be fullscreen. Should I pass Vector2.zero?
I meant, "Stretch to fit". Inside that Stretch to fit transparent panel I usually build every other kind of UI.
You haven't said where on screen you want to re-position the menu to, but the SetCentre function's parameter takes screen-space coordinates as a Vector2. Placing it in the centre of the screen would be a case of calling:
would this "stretch to fit the whole view (black bars excluded)" UI element take "forced aspect ratio" and aspect ratio in consideration?
should I put this on the canvas root of the menu, or on the first rectTransfom inside it?
Those particular values would place the menu in the centre, so it wouldn't be affected by the side-borders. Passing the second parameter as true would cause the vector to be in a co-ordinate space relative to the border, however - see its entry in the Scripting Guide.
UI menus are disabled when turned off, so the exact script above would be best placed on a separate GameObject entirely. You could, however, likely just call menu.SetCentre in the OnEnable function if placed on the Canvas root.
Thanks!
I actually slightly changed the above code to take 0-1 screen space coords into consideration.
Yes, I already had a look at that page, still, I'd love to know:
Here's the "generic" code I'm reusing for each UI Prefab in the game (because, yes, I have to put one on each of those, not sure if there's a way to "centralise" it making this setting game-wide or a flag to automatically handle each custom prefab UI appropriately)
It should make no difference if the script does not refer to the UI directly.
Such a script would be better-placed in the AC wiki, as it is a demonstration on how to use the API to get what you want.
As the function ultimately manipulates UI component values, this would be more of a question for Unity. I should think you'd be fine with both.
I've replied to the thread.