To implement the Auto Update SDK, include the following callback methods:
Method | Description |
---|---|
OnInitializationSuccess | Triggered when SDK initialization is successful. |
OnUpdateStatus | Triggered when a new version of the app is available, and immediately after invoking thecheckForUpdate() method. |
OnDownloadStarted | Triggered when permission to install has been granted by the player, and the download process begins. The SDK will attempt to download the new app version. |
OnProgress | Triggered when the download is initiated and when every new byte or packet is downloaded thereafter. You may use it to show download progression in real-time. |
OnDownloadEnded | Triggered when the download process is complete. |
OnInstallationReady | Triggered when the SDK is ready to start the installation process. |
OnError | Triggered when an error occurs during the update process, excluding initialization failures. Follow the instructions in the Unity Auto Update SDK troubleshooting article and try to re-initialize again based on the error. |
Example code
Below is the example code for the implementation:
using Appcharge.Updater;
using Appcharge.Updater.Interfaces;
using Appcharge.Updater.Models;
using UnityEngine;
using UnityEngine.UI;
public class Sample : MonoBehaviour, IUpdateCallback
{
[SerializeField] private Text _logger;
[SerializeField] private Button _btnCheckForUpdate;
[SerializeField] private Button _btnInstallUpdate;
private UpdateStatusModel _status;
private void Start()
{
UpdateController.Instance.Init("sandbox", "checkoutPublicKey", "customerId", this);
}
#region Interface Callback Methods
public void OnInitializationSuccess()
{
_logger.text = "Initialization successful.";
_btnCheckForUpdate.gameObject.SetActive(true);
}
public void OnUpdateStatus(UpdateStatusModel status)
{
_status = status;
_logger.text = $"A new version of your app is available: {status.Message}";
_btnInstallUpdate.gameObject.SetActive(true);
}
public void OnDownloadStarted()
{
_logger.text = "Downloading the new app version.";
}
public void OnProgress(int progress)
{
_logger.text = $"Progress: {progress}%";
}
public void OnDownloadEnded()
{
_logger.text = "The new app version has been downloaded.";
}
public void OnInstallationReady()
{
_logger.text = "The new app version has been downloaded and is ready to install.";
}
public void OnError(int errorCode)
{
_logger.text = $"Error: {errorCode}";
}
#endregion
public void CheckForUpdates()
{
UpdateController.Instance.CheckForUpdates();
}
public void InstallUpdate()
{
if (_status == null)
{
_logger.text = "No status";
return;
}
if (_status.IsUpdateAvailable)
{
UpdateController.Instance.InstallUpdate();
}
}
}