Address: http://blog.csdn.net/asd237241291/article/details/8126619
If you need to reprint the original article, please note: Reprinted from the off-mosoft unity3d learning tour this article link address: unity3d ngui adaptive screen resolution 1. uiroot: According to the height of adaptive screen resolution. The ngui root directory's uiroot component comes with a highly adaptive resolution feature. You can select three scaling policies for the scaling style attribute.
- Pixelperfect perfect pixels: displays the set pixels directly. Scaled down when the screen height is lower than minimum height, and scaled up when the screen height is greater than maximum height.
- Fixedsize scale proportionally: Scale proportionally based on the preset settings.
- Fixedsizeonmobiles in combination, Android and IoS are fixedsize, and others are in pixelperfect mode.
// Fixedsize: Specify the ideal resolution height.
// Fixedsizeofwidth: Specify the width of the ideal resolution.
Manual height:Follow the ideal resolution. When the game view (the screen resolution After packaging) is not the ideal resolution, proportional scaling is performed.
Minimum Height: The game view scales proportionally below this value.
Maximum height: The game view scales proportionally above this value.
All the three scaling methods calculate the zooming ratio based on the height and ignore the width completely.
At the time of creation, the UI ratio is based on the longest 16: 9 (red), and the other 3: 2 (green) is the content area. The two sides of the red color have different degrees of cutting on different proportions of mobile phones, so do not place the game content in this area. 2. uiroot: adaptive screen resolution based on the width. Uiroot has implemented the highly adaptive function, but my current requirement is to adapt according to the width, and leave blank when the screen height is higher than the UI height. 1. First, add a state to uiroot.
- Public Enum Scaling
- {
- Pixelperfect,
- Fixedsize,
- Fixedsizeonmobiles,
- /// <Summary>
- /// Fit according to the width
- /// </Summary>
- Fixedsizeofwidth,
- }
2. The implementation still requires the fixedsize algorithm, so you need to modify two judgment statements. 1:
- Public float getpixelsizeadjustment (INT height)
- {
- Height = mathf. Max (2, height );
- // Modify 1
- If (scalingstyle = scaling. fixedsize | scalingstyle = scaling. fixedsizeofwidth)
- Return (float) manualheight/height;
-
- # If unity_iphone | unity_android
- If (scalingstyle = scaling. fixedsizeonmobiles)
- Return (float) manualheight/height;
- # Endif
- If (height <minimumheight) Return (float) minimumheight/height;
- If (height> maximumheight) Return (float) maximumheight/height;
- Return 1f;
- }
Modify 2:
- Public int activeheight
- :{
- Get
- {
- Int Height = mathf. Max (2, screen. Height );
- // Modify 2
- If (scalingstyle = scaling. fixedsize | scalingstyle = scaling. fixedsizeofwidth)
- Return manualheight;
- # If unity_iphone | unity_android
- If (scalingstyle = scaling. fixedsizeonmobiles)
- Return manualheight;
- # Endif
- If (height <minimumheight) return minimumheight;
- If (height> maximumheight) return maximumheight;
- Return height;
- }
- }
3. added a width-based adaptive algorithm.
- Void Update ()
- {
- # If unity_editor
- If (! Application. isplaying & gameobject. layer! = 0)
- Unityeditor. editorprefs. setint ("ngui layer", gameobject. Layer );
- # Endif
- If (mtrans! = NULL)
- {
- Float calcactiveheight = activeheight;
- If (calcactiveheight> 0f)
- {
- Float size = 2f/calcactiveheight;
- // Here, here, here
- If (scalingstyle = scaling. fixedsizeofwidth)
- {
- Float radio = (float) screen. width/screen. height;
- Size = size * radio;
- }
- Vector3 ls = mtrans. localscale;
- If (! (Mathf. Abs (LS. X-size) <= float. epsilon) |
- ! (Mathf. Abs (LS. Y-size) <= float. epsilon) |
- ! (Mathf. Abs (LS. Z-size) <= float. epsilon ))
- {
- Mtrans. localscale = new vector3 (size, size, size );
- }
- }
- }
- }
3. uistretch: adaptive screen resolution based on the width. (Not supported by ngui3.0.7) This is a method for implementing adaptive differentiation rates for ngui in earlier versions. After the uiroot adaptive method is added to the new version, this script will not be officially recommended. In addition to the highly adaptive function, the style provided by this script also requires image stretching Based on the width and cannot meet our requirements. Basedonheight is the most suitable for us, so we can modify the basedonwidth according to this function. This function has been written in the previous blog. Now this article replaces the previous one, so I will post the modified content. First, add a basedonwidth in the style enumeration, Type
- Public Enum Style
- {
- None,
- Horizontal,
- Vertical,
- Both,
- Basedonheight,
- Basedonwidth,
- Fillkeepingratio,
- Fitinternalkeepingratio
- }
Add an if branch to the update method.
- If (style = style. basedonheight)
- {
- Localscale. x = relativesize. x * rectheight;
- Localscale. Y = relativesize. y * rectheight;
- } Else if (style = style. basedonwidth)
- {
- Localscale. x = relativesize. x * rectwidth;
- Localscale. Y = relativesize. y * rectwidth;
- }
- Else if (style = style. fillkeepingratio)
- {......}
This script is implemented by stretching scale, so this script should be placed on the UI you need to stretch (if you only need a background image to adapt to the screen resolution, add the script to the background image. If you want all the elements in a panel to be adaptive, place the script on the panel. If you want to make all the UIS adaptive resolution, put it on ngui's cameta .) For the UI camera attribute, You must select the camera that renders the current UI. Step 1: Set the game view to an ideal width (then scale according to this ratio .). 2. Select an object for uistretch as needed and add this component. Assign a value to the UI cameta. 3. Change the size of the UI cameta to the width of the current screen. (The scale X and Y of this object have been set to the screen width by uistrech, and this value cannot be modified .) 4. Change the window width at this time. Only the scale X and Y of the object have been automatically modified, and the UI view has been automatically adapted ~!