Jetpack Window Manager for Unity

Jetpack Window Manager is available as a package that can be included in your Unity project. Customize the gradle build system and implement a custom base activity class, and then add a C# helper class that calls the native Java API.

Foldable and dual-screen API

The WindowManagerHelper.cs code provides these methods that expose information supplied by the Jetpack Window Manager:

  • WindowManagerHelper.GetCurrentWindowMetricsBounds. The bounds (left, top, width, height) of the entire area the application occupies.
  • WindowManagerHelper.IsSeparating. Whether the folding feature is separating screen areas. This is always TRUE on the Surface Duo. It's TRUE on some other foldable devices only when they are folded but FALSE when they're flat.
  • WindowManagerHelper.Orientation. The orientation of the folding feature, if there is one. HORIZONTAL or VERTICAL.
  • WindowManagerHelper.OcclusionType. Whether the folding feature obscures content. FULL or NONE.
  • WindowManagerHelper.State. The state of the fold. FLAT or HALF_OPEN.
  • WindowManagerHelper.Bounds. The bounds (left, top, width, height) of the folding feature, if there is one.

Here's a screenshot of the API demonstration sample showing device information from the Jetpack Window Manager package:

Screenshot shows Jetpack Window Manager properties showing in the Unity sample app.

Implement Jetpack Window Manager in Unity

Follow these steps to customize your Android app build configuration and implement Jetpack Window Manager. You can also review the code in this sample.

  1. In Unity, go to Edit > Project Settingsā€¦ > Player > (Android) > Publishing Settings and select these Build options:

    Screenshot shows the Publishing Settings Build options list.

    This step places new items in the project's Assets/Plugins/Android, which are then customized to support the Window Manager package.

  2. Update the mainTemplate.gradle to include Jetpack Window Manager in the project, by adding the following dependencies:

    dependencies {
        implementation "androidx.window:window:1.0.0"
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'androidx.core:core-ktx:1.3.2'
    }
    
  3. Update the gradleTemplate.properties file by adding the following line:

    android.useAndroidX = true    
    
  4. The app needs a custom activity to implement the window manager functionality and expose information to C#. Create a file FoldablePlayerActivity.java in the same folder Assets/Plugins/Android, and then update AndroidManifest.xml to reference the new class:

    <activity android:name="com.microsoft.device.dualscreen.unity.FoldablePlayerActivity"
    
  5. Add the complete class from FoldablePlayerActivity.java on GitHub.

  6. Copy the WindowManagerHelper.cs helper class from GitHub into the project. The folder is Assets/SurfaceDuo/Plugins in the sample.

The Assets/Plugins/Android now contains these items, customized in the steps above:

Screenshot shows Customized Android project configuration files in Unity.

The WindowManagerHelper.cs file is in the Assets/SurfaceDuo/Plugins folder in the sample, but this helper class can be located anywhere in your game code.

Once you have implemented layout changes based on the presence of a folding feature, you can test your game on Surface Duo and other foldable devices that support Jetpack Window Manager.

Next steps

Review these samples.