How to create In-App Updates for Android in C# Unity

In this tutorial, I will illustrate how to create In-App Updates so that when a user opens the app, the app will automatically check if there is an updated version of the app and request the user to install the updated version.

To do this, go to Package Manager >> My Registries and install Google Play In-app Updates by Google LLC. Force resolve if there are package conflicts. If you have already integrated Ads into the app then there may be no extra package conflicts.

Next, I will create a script named OnStartHome that contains the class of the same name.

Imports

These are the namespaces included to use specific features and libraries (GoogleMobileAds.Api is for mobile ads):

using GoogleMobileAds.Api;
using UnityEngine;
using System;
using Google.Play.AppUpdate;
using Google.Play.Common;

OnStartHome Class

This is a Unity script that manages the user interface and checks for app updates on startup:

public class OnStartHome : MonoBehaviour
{

    private AppUpdateManager _appUpdateManager;

Start Method

This method runs when the script is first executed. It initializes the AppUpdateManager, checks for updates, before initializing the Google Mobile Ads SDK:

    void Start()
    {
        _appUpdateManager = new AppUpdateManager();
        CheckForImmediateUpdate();

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(initStatus => { });
    }

CheckForImmediateUpdate Method

This method checks if there’s an available app update. It requests update information and, if an update is available, it calls StartImmediateUpdate:

    private void CheckForImmediateUpdate()
    {
        // Request update information
        var appUpdateInfoOperation = _appUpdateManager.GetAppUpdateInfo();

        // Attach a callback to handle the result
        appUpdateInfoOperation.Completed += appUpdateInfo =>
        {
            if (appUpdateInfo.Error != AppUpdateErrorCode.NoError)
            {
                Debug.LogError("Failed to get update info: " + appUpdateInfo.Error);
                return;
            }

            // Check if an update is available and if it can be performed immediately
            var updateInfo = appUpdateInfo.GetResult();
            var immediateUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

            if (updateInfo.UpdateAvailability == UpdateAvailability.UpdateAvailable &&
                updateInfo.IsUpdateTypeAllowed(immediateUpdateOptions))
            {
                StartImmediateUpdate(updateInfo);
            }
        };
    }

StartImmediateUpdate Method

This method performs an immediate app update. It logs that the update is starting, creates options for the immediate update, starts the update process, and logs the result of the update process:

    private void StartImmediateUpdate(AppUpdateInfo appUpdateInfo)
    {
        Debug.Log("Starting immediate update...");

        // Create AppUpdateOptions for immediate update
        var appUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

        var immediateUpdateOperation = _appUpdateManager.StartUpdate(appUpdateInfo, appUpdateOptions);

        // Attach a callback to handle the result of starting the update
        immediateUpdateOperation.Completed += updateResult =>
        {
            if (updateResult.Error != AppUpdateErrorCode.NoError)
            {
                Debug.LogError("Immediate update failed: " + updateResult.Error);
            }
            else
            {
                Debug.Log("Immediate update completed successfully.");
            }
        };
    }
}

This script is designed to manage the app’s UI and ensure the app is up-to-date with the latest version. Just create an object in the Home scene, attach the script to the object and then you’re good to go ?!

Whole code (click to download):

using GoogleMobileAds.Api;
using UnityEngine;
using System;
using Google.Play.AppUpdate;
using Google.Play.Common;


public class OnStartHome : MonoBehaviour
{
    public Player player;

    private AppUpdateManager _appUpdateManager;

    void Start()
    {
        _appUpdateManager = new AppUpdateManager();
        CheckForImmediateUpdate();

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(initStatus => { });
    }

 

    private void CheckForImmediateUpdate()
    {
        // Request update information
        var appUpdateInfoOperation = _appUpdateManager.GetAppUpdateInfo();

        // Attach a callback to handle the result
        appUpdateInfoOperation.Completed += appUpdateInfo =>
        {
            if (appUpdateInfo.Error != AppUpdateErrorCode.NoError)
            {
                Debug.LogError("Failed to get update info: " + appUpdateInfo.Error);
                return;
            }

            // Check if an update is available and if it can be performed immediately
            var updateInfo = appUpdateInfo.GetResult();
            var immediateUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

            if (updateInfo.UpdateAvailability == UpdateAvailability.UpdateAvailable &&
                updateInfo.IsUpdateTypeAllowed(immediateUpdateOptions))
            {
                StartImmediateUpdate(updateInfo);
            }
        };
    }

    private void StartImmediateUpdate(AppUpdateInfo appUpdateInfo)
    {
        Debug.Log("Starting immediate update...");

        // Create AppUpdateOptions for immediate update
        var appUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

        var immediateUpdateOperation = _appUpdateManager.StartUpdate(appUpdateInfo, appUpdateOptions);

        // Attach a callback to handle the result of starting the update
        immediateUpdateOperation.Completed += updateResult =>
        {
            if (updateResult.Error != AppUpdateErrorCode.NoError)
            {
                Debug.LogError("Immediate update failed: " + updateResult.Error);
            }
            else
            {
                Debug.Log("Immediate update completed successfully.");
            }
        };
    }

}

Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!