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 alwaysTRUE
on the Surface Duo. It'sTRUE
on some other foldable devices only when they are folded butFALSE
when they're flat.WindowManagerHelper.Orientation
. The orientation of the folding feature, if there is one.HORIZONTAL
orVERTICAL
.WindowManagerHelper.OcclusionType
. Whether the folding feature obscures content.FULL
orNONE
.WindowManagerHelper.State
. The state of the fold.FLAT
orHALF_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:
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.
In Unity, go to Edit > Project Settings… > Player > (Android) > Publishing Settings and select these Build options:
This step places new items in the project's Assets/Plugins/Android, which are then customized to support the Window Manager package.
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' }
Update the gradleTemplate.properties file by adding the following line:
android.useAndroidX = true
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"
Add the complete class from FoldablePlayerActivity.java on GitHub.
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:
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.