Search Unity

Force Screen Resolution 480x320 on 3.2 (part 2)

Discussion in 'Android' started by robhuhn, Feb 16, 2011.

  1. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    Hi,

    because of several reasons (performance among other reasons) I want a screen resolution of 480x320.
    I set following properties in the AndroidManifest.xml like described in this thread.

    Code (csharp):
    1.  
    2.         <supports-screens
    3.         android:smallScreens="false"
    4.         android:normalScreens="true"
    5.         android:largeScreens="false"
    6.         android:anyDensity="false"/>
    With this settings I have a border on the right of my screen as expected (nexus one). In Unity 3.2 the main frame is rendered twice in vertical alignment within this border... that was not expected.
    What I want is a black border left and right. I can achieve that when I add this line to my main activity:

    Code (csharp):
    1. getWindow().setLayout(480, 320);
    Now it looks exactly like I want it. Unfortunately I get the titlebar as soon as I add that line and android:theme="@android:style/Theme.NoTitleBar" takes no effect anymore.

    I tried to hide the titlebar programmatically with no success.


    Has anybody a solution for this issue or any ideas how it could be solved?
     
  2. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    I figured out that if I add
    Code (csharp):
    1. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    to the androidmanifest.xml file the titlebar is hidden the first time and I have a window dimension of 480x320. That's what I wanted to achieve.

    Unfortunately it works only the first time I start the app. The titlebar is back again on the second launch (without compiling) - definitely the most stubborn titlebar I've ever seen.
     
  3. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    Finally it's solved. Adding these lines in onCreate fixes the issue.

    Code (csharp):
    1. @Override
    2. protected void onCreate(Bundle savedInstanceState)
    3. {
    4.     Window window = getWindow();
    5.     window.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL);
    6.     window.setLayout(480, 320);
    7.     window.requestFeature(Window.FEATURE_NO_TITLE);
    8.     window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    9.        
    10.     super.onCreate(savedInstanceState);
    11. }
    I know that it is not a Unity issue but an Android issue. Anyhow I posted the issue because I can imagine that some Unity game developers want to set low res on Android to get some more performance.

    @edit:
    Hm, it's not centered anymore.
     
    Last edited: Feb 24, 2011
  4. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    I didn't find a solution. Here is a crappy hack but the only way I get it to work.
    I sent a bug report to Android.

    Code (csharp):
    1. /*
    2.  * (non-Javadoc)
    3.  *
    4.  * @see android.app.Activity#onStop()
    5.  */
    6. @Override
    7. protected void onStop()
    8. {
    9.     super.onStop();
    10.  
    11.  
    12.     //hack to prevent stopping fullscreen mode if the home button was pressed
    13.     ActivityManager am = (ActivityManager) this
    14.                     .getSystemService(ACTIVITY_SERVICE);
    15.     List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    16.     ComponentName componentInfo = taskInfo.get(0).topActivity;
    17.  
    18.     if(componentInfo.getShortClassName().equalsIgnoreCase(".Launcher"))
    19.     {
    20.         onDestroy();
    21.     }
    22. }
    23.  
    24.  
    25. @Override
    26. public void onDestroy()
    27. {
    28.     super.onDestroy();
    29.     System.runFinalizersOnExit(true);
    30.     System.exit(0);
    31. }
     
  5. Wozik

    Wozik

    Joined:
    Apr 10, 2009
    Posts:
    662
    did you also try Screen.setResolution() call from Unity scripts?
     
  6. LumaPxxx

    LumaPxxx

    Joined:
    Oct 3, 2010
    Posts:
    339
    Screen.setResolution() could do it but not in full screen, at least on my phone.
     
  7. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    Yes, Screen.setResolution() took no effect unfortunately. It is an Android or Device bug anyway. I had several test cases with a non-unity project.
     
    Last edited: Feb 25, 2011
  8. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    I have tried this on a nexus one and if you call Screen.setResolution() in a Start function it will force the back buffer resolution to what you set it to. The application will then display fullscreen in the desired resolution.
     
  9. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    On my Galaxy S, everything is stretched even when I set the resolution to 320 x 480. So I set camera's pixelRect width and height and it works pretty well for me.

    Code (csharp):
    1.  
    2. function Start()
    3. {
    4.     if( Screen.currentResolution.height > 480 )
    5.     {
    6.         this.camera.pixelRect = Rect(0,
    7.                                      (Screen.currentResolution.height - Screen.currentResolution.width*1.5)*0.5,
    8.                                      Screen.currentResolution.width,
    9.                                      Screen.currentResolution.width*1.5);
    10.     }
    11. }
    12.  
    This will center the game screen with black borders on the top and the bottom.
     
    Last edited: Apr 25, 2011
  10. kiranmaya

    kiranmaya

    Joined:
    May 27, 2010
    Posts:
    218
    do we can force the set the resolution for any phone resolution to our resolution? i have coded the menu gui controls for 320*480, if we set like yours,does it menu stretched automatically on higher resolution like xperia arc?