Defold supports GUIs that automatically adapt to screen orientation changes on mobile devices. By using this feature you can design GUIs that adapt to the orientation and aspect ratio of a range of screen sizes. It is also possible to create layouts that match particular device models.
By default, thegame.project settings specify that a built-in display profiles settings file (“builtins/render/default.display_profiles”) is used. The default profiles are “Landscape” (1280 pixels wide and 720 pixels high) and “Portrait” (720 pixels wide and 1280 pixels high). No device models are set on the profiles so they will match on any device.
To create a new profiles settings file, either copy the one from the “builtins” folder orright click a suitable location in theAssets view and selectNew... ▸ Display Profiles. Give the new file a suitable name and clickOk.
The editor now opens the new file for editing. Add new profiles by clicking the+ in theProfiles list. For each profile, add a set ofqualifiers for the profile:
iPhone10
will match “iPhone10,*” models. Model names with commas should be enclosed in quotes, i.e."iPhone10,3", "iPhone10,6"
matches iPhone X models (seeiPhone wiki). Note that the only platforms reporting a device model when callingsys.get_sys_info()
is Android and iOS. Other platforms return an empty string and will therefore never pick a display profile that has a device model qualifier.You also need to specify that the engine should use your new profiles. Opengame.project and select the display profiles file in theDisplay Profiles setting underdisplay:
If you want the engine to automatically switch between portrait and landscape layouts on device rotation, check theDynamic Orientation box. The engine will dynamically select a matching layout and also change the selection if the device changes orientation.
The current set of display profiled can be used to create layout variants of your GUI node setup. To add a new layout to a GUI scene, right-click theLayouts icon in theOutline view and selectAdd ▸ Layout ▸ ...:
When editing a GUI scene, all nodes are edited on a particular layout. The currently selected layout is indicated in the GUI scene layout dropdown in the toolbar. If no layout is chosen, the nodes are edited in theDefault layout.
Each change to a node property that you do with a layout selectedoverrides the property in respect to theDefault layout. Properties that are overridden are marked in blue. Nodes with overridden properties are also marked in blue. You can click on the reset button next to any overridden property to reset it to the original value.
A layout cannot delete or create new nodes, only override properties. If you need to remove a node from a layout you can either move the node off-screen or delete it with script logic. You should also pay attention to the currently selected layout. If you add a layout to your project, the new layout will be set up according to the currently selected layout. Also, copying and pasting nodes considers the currently selected layout, when copyingand when pasting.
The dynamic layout matching scores each display profile qualifier according to the following rules:
If there is no device model set, or the device model matches, a score (S) is calculated for the qualifier.
The score (S) is calculated with the area of the display (A), the area from the qualifier (A_Q), the aspect ratio of the display (R) and the aspect ratio of the qualifier (R_Q):
The profile with the lowest scoring qualifier is selected, if the orientation (landscape or portrait) of the qualifier matches the display.
If no profile with a qualifier of the same orientation is found, the profile with the best scoring qualifier of the other orientation is selected.
If no profile can be selected, theDefault fallback profile is used.
Since theDefault layout is used as fallback in runtime if there are no better matching layout it means that if you add a “Landscape” layout, it will be the best match forall orientations until you also add a “Portrait” layout.
When the engine switches layout as a result of device rotation, alayout_changed
message is posted to the GUI components’ scripts that are affected by the change. The message contains the hashed id of the layout so the script can perform logic depending on which layout is selected:
functionon_message(self,message_id,message,sender)ifmessage_id==hash("layout_changed")andmessage.id==hash("My Landscape")then-- switching layout to landscapeelseifmessage_id==hash("layout_changed")andmessage.id==hash("My Portrait")then-- switching layout to portraitendend
In addition, the current render script receives a message whenever the window (game view) changes and this includes orientation changes.
functionon_message(self,message_id,message)ifmessage_id==hash("window_resized")then-- The window was resized. message.width and message.height contain the-- new dimensions of the window.endend
When orientation is switched, the GUI layout manager will automatically scale and reposition GUI nodes according to your layout and node properties. In-game content, however, is rendered in a separate pass (by default) with a stretch-fit projection into the current window. To change this behavior, either supply your own modified render script, or use a cameralibrary.
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB