Address: http://blog.csdn.net/lzhq1982/article/details/18814023
This article is reprinted. Previously I used uistretch for screen self-adaptation, but there have been two hard injuries that make me uncomfortable. First, after the screen is stretched at will, the UI inside is not the proportion I want, obviously a circle is changed to an elliptical shape, and a tall beauty is changed to a fat sister. Well, if this can be tolerated, the second one really made me try all kinds of methods, that is, the Panel can't be cropped, and I can't do it if I want to make a scroll view, then Baidu knows that to crop a panel, both the scale and the scale of its parent to the root node are required (, 1). How can this be stretched, then I saw various code attempts from many of my friends, and the results failed one by one. It was difficult to use uistretch for screen self-adaptation. A new ngui version 3.0.8 was launched, uistretch is no longer available. It seems that it is not supported by the official team. Fortunately, there are other methods. I read a friend's blog and wrote it very clearly. I will forward it here, it helped me solve all the problems:
I have read some articles and most of them use uistretch. To be honest, I don't like this script.
I have always recommended the manualheight of uiroot for those who have problems adapting to the screen.
Provide three. Check whether the effect is what you want. You need to discuss the background with planning and art.
1. Normal Development resolution:
2. finer-looking resolutions:
3. Wider Resolution:
Note:
1. Develop and plan the resolution during development. This is important to ensure that all UIS are created at the same resolution.
2. Put this script on uiroot. Change the scaling style of uiroot to fixedsize.
3. aspectratioheight and aspectratiowidth are the development height and width.
4. Every uiroot needs to adjust manualheight to the height of planning.
5. Adjust the unity3d game window to the corresponding resolution.
(Thanks to Chengdu-daqiang for providing the following versions.Note: uicamera. onscreenresize is of version 3.0 +. Delete it if an error is reported.)
[CSHARP]View plaincopy
- Using unityengine;
- [Executeineditmode]
- [Requirecomponent (typeof (uiroot)]
- Public class szuirootscale: monobehaviour
- {
- Public int aspectratioheight;
- Public int aspectratiowidth;
- Public bool runonlyonce = false;
- Private uiroot mroot;
- Private bool mstarted = false;
- Void awake ()
- {
- Uicamera. onscreenresize + = screensizechanged;
- }
- Void ondestroy ()
- {
- Uicamera. onscreenresize-= screensizechanged;
- }
- Void start ()
- {
- Mroot = nguitools. findinparents <uiroot> (this. gameobject );
- Mroot. scalingstyle = uiroot. Scaling. fixedsize;
- This. Update ();
- Mstarted = true;
- }
- Void screensizechanged ()
- {
- If (mstarted & runonlyonce ){
- This. Update ();
- }
- }
- Void Update ()
- {
- Float defaultaspectratio = aspectratiowidth * 1f/aspectratioheight;
- Float currentaspectratio = screen. Width * 1f/screen. height;
- If (defaultaspectratio> currentaspectratio ){
- Int horizontalmanualheight = mathf. floortoint (aspectratiowidth/currentaspectratio );
- Mroot. manualheight = horizontalmanualheight;
- } Else {
- Mroot. manualheight = aspectratioheight;
- }
- If (runonlyonce & application. isplaying ){
- This. Enabled = false;
- }
- }
- }