When we develop mobile games, it is common to choose the constrained/fixedsize zoom mode to ensure that the image is consistent with the size of the screen at different resolutions, but it is not enough for screen adaptation. Different phones have different aspect ratios, so we need to introduce the concept of relative position.
As a mature UI plugin, Ngui provides us with a proven screen-adaptive technology.
Anchor components
We can create a anchor component to the stage, the anchor is the anchor point, it can be designated as the camera's 9 points (respectively: top left, top, right, left, middle, right, bottom left, bottom and bottom right) for its anchor point, when the size of the camera changes (that is, the screen size changes), The anchor component can correct its current position, and the component's relative positioning is achieved by dragging components that need relative positioning into the stroke component.
A few things to note:
- The Runonlyonce property, which determines whether the game window size will not change during the run of the mobile game, so it can be checked if it is only applied at the beginning.
- Anchor components do not need to be dragged manually.
Pivot Properties for Components
That is, the center point of the component or the registration point, the rotation offset of the component depends on that point and can be considered as the local origin of the component.
Component's Anchors Property
Unlike the anchor component, the anchors property is a relatively positioned property that is provided by all the underlying components.
The effect is to have the position and dimensions of the current component relative to another component, and if the target component changes, these changes will affect the relative component.
The anchors property can set the relative component that is the position of the four sides of the component.
Ngui Study Notes (iii): Screen adaptive