Unity Ngui's multi-resolution adaptation

Source: Internet
Author: User

Reference Link: Http://blog.csdn.net/mfc11/article/details/17681429,CSDN mfc11

1, Ngui The default adaptation method:

Ngui default is the adaptation method is based on the height of the adaptation, the height of the specific settings can be see the connection: http://www.cnblogs.com/vitah/p/3942392.html.

2. Problems that may occur with the default adaptation method:

When the aspect ratio of the target device is consistent with the aspect ratio of the edited page, the entire UI is displayed perfectly, and the page width is larger than the width of the device when the target device aspect ratio is less than the edited aspect ratio, so that the extra portion is not displayed and the page width is smaller than the device width when the target device aspect ratio is greater Black edges appear on both sides of the device.

3. Using Uianchor:

Uianchor is the entire page is divided into topleft/top/topright/left/center/right/bottomleft/bottom/bottomright nine regions, The nodes that mount the Uianchor component are automatically docked to the appropriate zone according to the settings. With Uianchor, the above two problems will be resolved to a certain extent: when the target device aspect ratio is less than the editing aspect ratio, because of the Uianchor automatic docking function, the UI will not be trimmed off, but the left and right spacing between the UI will be correspondingly smaller, there may be UI overlap problems When the target device aspect ratio is greater than the edit aspect ratio, the left and right spacing between the UI becomes larger, so that at least there is no UI cut or overlapping.

This method of using Uianchor only needs to solve the possible UI overlap, but when there is a full-screen sprite, a full screen uisprite regardless of whether or not to use Uianchor, the target device aspect ratio is more than an hour, the sprite will be trimmed horizontally , and when the target device aspect ratio is larger, the sprite does not fill the entire screen.

4. You need to adjust the UI display when using Uianchor:

A summary of the three questions:

(1) Device width and height compared to the UI of the aspect ratio of the hour, UI overlap problem;

(2) The device is wide and high compared to the UI is small, full-screen sprite is cut;

(3) device width and height compared to the UI wide, full-screen sprite can not be covered full screen;

5, the device width and height compared to the UI of the aspect ratio of more than the hour, the overlap problem of the UI solution:

When the device is much smaller than the UI aspect ratio, the UI root adjusts the scale size based on the height of the UI, leaving the device wide enough to display all of the UI content, and we can adjust the camera's camera.orthographicsize (2D GUI only). Enough to display the width of the page. That is, changed the Ngui original "height adaptation" principle, to "width adaptation", so that the entire page can be displayed, and because of the existence of Uianchor, the left and right spacing of the UI remains unchanged, but the upper and lower spacing will become larger. Give the code to mount the Camere object under the UI root:

usingUnityengine;usingSystem.Collections; Public classuiadjust:monobehaviour{//The width and height of the given UI    Private floatStandard_width =1280f; Private floatStandard_height =720f; //width and height of the device    Private floatDevice_width =0f; Private floatDevice_height =0f;  Public voidAwake () {//get the device's width and heightDevice_width =Screen.width; Device_height=Screen.height;    Setcamerasize (); }    Private voidsetcamerasize () {floatAdjustor =0f; //aspect ratio of the UI        floatStandard_aspect = standard_width/Standard_height; //aspect ratio of the device        floatDevice_aspect = device_width/Device_height; //The device aspect ratio is less than the aspect ratio of the UI, which adjusts the orthographicsize of the camere so that the width of the device can display all the UI, i.e., on the basis of height adaptation and then on a width -fitting        if(Device_aspect <standard_aspect) {Adjustor= Standard_aspect/Device_aspect; Camera.orthographicsize=adjustor; }    }}
View Code

The appeal code can solve the non-full-screen UI adaptation problem;

6, on the basis of 5 steps, full screen sprite display problem:

On the basis of 5 steps, the display of a full-screen sprite will have two problems:

(1) The device aspect ratio is more than the hour, the sprite does not fill up and down:

(2) When the device aspect ratio is larger, the sprite is not filled;

At this time we adjust the size of the full-screen sprite according to the corresponding proportion of the upper and lower direction and left and right of the scale value can be, if the sprite can be stretched, if it is not stretched sprite need to use other methods, give full-screen sprite adaptation code, To mount under a sprite that requires full-screen display:

usingUnityengine;usingSystem.Collections; Public classFullscreenspriteadjust:monobehaviour {Private floatStandard_width =1280f; Private floatStandard_height =720f; Private floatDevice_width =0f; Private floatDevice_height =0f; voidAwake () {Device_width=Screen.width; Device_height=Screen.height;    Spriteadjust (); }    voidSpriteadjust () {//picture standard aspect ratio        floatStandard_aspect = standard_width/Standard_height; //aspect ratio of the device        floatDevice_aspect = device_width/Device_height; floatscale_x =1; floatScale_y =1; //device aspect ratio is greater than picture aspect ratio, Sprite does not fill around        if(Device_aspect >standard_aspect) {scale_x= Device_aspect/Standard_aspect; }        //device aspect ratio is less than picture aspect ratio, Sprite does not fill up or down        Else if(Device_aspect <standard_aspect) {scale_y= Standard_aspect/Device_aspect; } GameObject.transform.localScale=NewVector3 (scale_x, scale_y, gameobject.transform.localscale.z); }}
View Code

The content of the article is almost a copy of the reference link, thanks to the author's ideas. Code is written by yourself, if there is a better way to leave a message or e-mail: linw1225#163.com (#换成 @) ~

Unity Ngui's multi-resolution adaptation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.