Access dual-screen info on Surface Duo from Unity

Note

New Unity projects should use Jetpack Window Manager for foldable device features. It works for Surface Duo, Surface Duo 2, and devices from other manufacturers.

The Microsoft dual-screen SDK described below works only on Microsoft Surface Duo devices.

The Microsoft dual-screen SDK is available as a package that can be included in your Unity project. Customize the gradle build system and then add a C# helper class that calls the native Java API.

Microsoft dual-screen SDK for Surface Duo

The SurfaceDuoScreenHelper.cs code provides methods that expose information supplied by the Microsoft SDK for Surface Duo:

  • DeviceHelper.IsDualScreenDevice. Call this method before accessing other dual-screen APIs.
  • ScreenHelper.GetCurrentRotation. Get the current rotation value. The value 0 means 0 degrees, 1 means 90 degrees, 2 means 180 degrees, and 3 means 270 degrees.
  • ScreenHelper.IsDualMode. Whether the app is spanned across both screens. The viewport is partially obscured by the gap between screens.
  • ScreenHelper.GetScreenRectangles. Returns the dimensions of the two screens in pixels.
  • ScreenHelper.GetHinge. Returns the dimensions of the obscured area of the viewport in pixels. If the app is not spanned, this returns zero-dimensions. Since it is only showing on a single screen, nothing is being obscured.

View sample code for these methods in the Unity ScreenHelper sample. The sample displays device information, as shown in the screenshot below:

Screenshot shows a Unity game sample screen data.

Implement the Microsoft dual-screen SDK in Unity

An example of this integration is available on the 2019-LTS-archive branch of the Unity samples repository.

  1. Update mainTemplate.gradle to download the Surface Duo Android SDK:

    maven {
        url 'https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1'
    }
    

    and

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
        implementation "com.microsoft.device:dualscreen-layout:0.9.0"
    }
    
  2. Copy this script SurfaceDuoScreenHelper.cs into your game project.

  3. Use the dual screen methods listed on this page to enhance your game for dual-screen devices.

Next steps

Look at Unity game samples for Surface Duo.