Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Configure an instant app for a game using the Google Play Instant Unity plugin

The steps for setting up apps to run on Google Play Instant, as explained in Create your first instant app, also apply to games. This guide emphasizes some setup steps specific to games.

The Google Play Instant Unity plugin configures your project to create an instant app version of your game.

This guide focuses on optimizing your game's download size and ensuring that an install prompt appears when players interact with the instant app version of your game. Before completing the steps on this page, learn how to set up the plugin using the project's GitHub page.

Distribute your game's content into asset bundles

If only part of your game is available as an instant app, it's helpful for the user to download your entire game one part at a time. To make this process possible, create an instant app that contains only a simplified loading screen. When the user starts this instant app, your app can then immediately start downloading an asset bundle, or a package of resources, that contains the main game scene from a content delivery network (CDN). Using this strategy, you delay the downloading of most of your game's resources to the time when the player first opens your game.

To implement this distributed downloading solution, complete the following steps:

  1. Use the Asset Bundle Browser plugin to create the asset bundle that represents your game's main scene.
  2. Upload this asset bundle to the location where you serve it to players, ideally using a CDN to reduce download latency. Possible locations could be the following:

  3. Create an instant app that contains the loading screen scene. From this loading screen, your game downloads the asset bundle that contains your game's main scene.

    The following code snippet demonstrates how to download the Asset Bundle and load the scene:

    IEnumerator Start () {
        // Download and load both required scenes.
        yield return StartCoroutine(
            DownloadAsset ("https://ASSETBUNDLE_URL", true));
    }
    
    IEnumerator DownloadAsset(string sceneURL, bool loadScene) {
        // Downloads and loads scenes.
        WWW bundleWWW = WWW.LoadFromCacheOrDownload(sceneURL, 0);
        yield return bundleWWW;
        var assetBundle = bundleWWW.assetBundle;
        if (loadScene) {
            if (assetBundle.isStreamedSceneAssetBundle) {
                string[] scenePaths = assetBundle.GetAllScenePaths();
                string sceneName =
                    System.IO.Path.GetFileNameWithoutExtension(scenePaths[0]);
                SceneManager.LoadScene(sceneName);
            }
        }
    }
    

Optimize your instant app's size

Use Player Settings in the Unity plugin

The Google Play Instant Unity plugin includes the Player Settings dialog, shown in Figure 1, which displays suggestions that help you optimize support for Google Play Instant, develop against more compatible graphics API, and reduce your APK's size.

Specific suggestions include using OpenGLES2 only, disabling
  Multithreaded Rendering, and setting the target SDK version to 26 or
  higher.
Figure 1. The Player Settings dialog

Use Android Player Settings in Unity

If you're using Unity directly, you can reduce the size of your game's instant app by using the Android Player Settings window instead. This window features the following options that you can toggle on and off:

  • Use IL2CPP as scripting backend and enable engine stripping.
  • Use .NET 2.0 Subset as the API compatibility level.
  • Select only ARM under device filtering instead of including both ARM and x86 in the instant app.

Show an install prompt

The Google Play Instant Unity plugin automatically configures your project to use Gradle as the build system. To display an install prompt in your game, however, you need to add some logic in your game.

To display the install prompt in your game's instant app, add the following code to your Install button's click handler:

using GooglePlayInstant;
...
public void Install() {
    // The requestCode referenced in the onActivityResult() callback to the
    // activity. Extend UnityPlayerActivity and check for the specified
    // requestCode integer to know whether the dialog was cancelled. Use
    // UnityPlayer.UnitySendMessage() to relay the response from your Android
    // app's code back to Unity scripts.
    const int requestCode = 123;

    // The activity that should launch the store's install dialog.
    using (var activity = InstallLauncher.GetCurrentActivity())

    // The intent to launch after the instant app has been installed.
    // Must resolve to an activity in the installed app package, or it cannot
    // be used.
    using (var postInstallIntent =
            InstallLauncher.CreatePostInstallIntent(activity)) {
        InstallLauncher.ShowInstallPrompt(activity, requestCode,
            postInstallIntent, null);
    }
}