"Turn" Unity3d Research Institute use Android's hardware scaling technology to optimize execution efficiency

Source: Internet
Author: User

Andoird too headache, especially some machines are screen big hardware conditions are rotten, in order to optimize rendering efficiency can try Android hardware scaling technology, I have been tested on the real machine, the efficiency really improved a lot, fps straight up.

The principle is as follows http://android-developers.blogspot.it/2013/09/using-hardware-scaler-for-performance.html

But I need to get the unity Android Glsurfaceview, but I tried a lot of ways is not to get it, but fortunately unity directly provides the method.

Find a suitable place to call the following method, which is supported on both iOS and Android, but it is not necessary to test iOS, and it is necessary to use it on Android.

Screen.setresolution (960,640,true);

This forces the resolution of the screen to be specified as 960x640. But your target device resolution may not be 3:2, so you need to make an algorithm, assuming that the interface is 960X640, at this time running in the 1280x760 resolution, 1280x760 and 960X640 and other than the conversion can be. Code I will not post ... Use the following code to remove the current phone's screen resolution, based on the algorithm to calculate the line.

int width = Screen.currentResolution.width;
int height = Screen.currentResolution.height;

It is also important to note that after my test found the horizontal screen game when Android cut back into the background in the foreground render area into a vertical screen. So every time you cut to the front desk must be forced to set the resolution.

1234567 void onapplicationpause(bool paused) {Logger. Print("Paused:", paused); if (! Paused) { Screen . Setresolution(960,640,true); }}

Another problem is when calling Screen.setresolution (960,640,true); Opening the Ngui interface immediately after it may cause the UI to blink. I think it's best to set the resolution in the Java inner layer through Unity's glsurfaceview, but I don't know how to get to Glsurfaceview, or if a friend knows please leave a message below and let me know.

To make a relatively simple interface, the interface is written on "Loading ..." A few characters, because the background is the black flash on the contrary, we plan to say can endure.

123456 IEnumerator Start() {Screen . Setresolution(960,640,true); yield return new waitforseconds(0.1f); //In the following code after processing set resolution logic}

Maybe I wrote screen.setresolution (960,640,true); May mislead you, I give an example. At the time of development, the fabric interface is 960X640, which runs on 1280x720. Then the modified resolution is screen.setresolution (1138,640,TRUE);

Because 1138 = 1280/(720/640) but you have to consider a situation larger than 960X640 or smaller than 960X640. In short, the calculated target resolution is equal to the ratio. If one is 960 the other is prorated, if one is 640 the other is also prorated.

Setting the resolution will not affect the original logic of your code, the efficiency has really improved a lot, it is recommended to use Android on the best. Hey. Welcome everyone in the following to the @ Rain Pine Momo message, we discuss together.

I am adding: This method does not affect the Ngui adaptive, nor does it affect any of the original logic of the game or change the code, the various Android resolutions are feasible, the efficiency can be improved a lot. Our project has been used vigorously, the only drawback is that when setting the resolution of the screen will flash a bit. You can do a black screen or a few words "loading in". So I want to change it in the Java layer, but I can't get glsurfaceview. If you have a friend to know please leave a message to tell me. Thank you.

The two-day Test found that sometimes press the phone's shutdown button, and then return to the game to find the game screen vertical (we are a horizontal screen game). And it's random, and the effect is as follows:

I found the answer in these two articles.

Http://blog.k-res.net/archives/1702.html

Http://stackoverflow.com/questions/7185644/android-opengl-crazy-aspect-ratio-after-sleep

To solve this problem needs to be in the Java layer to match, need to change your Unityplayernativeactivity.java

12345678910111213141516171819202122232425 private int width,height; //Setup activity Layout@Override protected void onCreate (Bundle savedinstancestate) {requestwindowfeature(Window. Feature_no_title); Super. OnCreate(savedinstancestate); GetWindow(). Takesurface(null); setTheme(android. R. Style. Theme_notitlebar_fullscreen); GetWindow(). SetFormat(pixelformat. rgb_565); munityplayer = new UnityPlayer (this if ( Munityplayer. Getsettings (.< Span class= "CRAYON-E" >getboolean (, true ) ) GetWindow (). SetFlags (windowmanager. Layoutparams. Flag_fullscreen, WindowManager. Layoutparams. Flag_fullscreen); Setcontentview(munityplayer); munityplayer. Requestfocus(); //Save the width and height of the game screendisplaymetrics dm = getresources(). Getdisplaymetrics(); width = dm. Widthpixels; height = dm. Heightpixels; }

It's still in this class.

123456789101112 //This ensures the layout would be correct.@Override public void onconfigurationchanged(Configuration newconfig ){Super. Onconfigurationchanged(newconfig); //Here force setting the width and height of the screenmunityplayer. Getview (. Getlayoutparams (. Width = width munityplayer. Getview (. Getlayoutparams (. Height = height Log. V("Unity", width+" + height" ); munityplayer. Configurationchanged(newconfig); }

Well, at present I use this method has been perfectly solved this problem, if you have any good experience to share, welcome to the following message to me.

    • This article fixed link: http://www.xuanyusong.com/archives/3205
    • Reprint Please specify: Rain pine Momo September 24, 2014 Yu Yussong Momo Program Research Institute published

"Turn" Unity3d Research Institute use Android's hardware scaling technology to optimize execution efficiency

Related Article

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.