Skip to main content

AR Lifecycle Management

Sometimes we don't want to run AR sessions all the time with our app, and there might be other scenes that only contains UIs. In this case it is better to deinitialize all AR related subsystems to save power and not have the batteries drained quickly, and only initialize them when the app is using it. This guide will explain about how we can manage the lifecycle of AR sessions. Since Pretia SDK is built on top of Unity's XR subsystems, we will look into how AR Foundation provides lifecyle management first.

Automatic Lifecycle Management

AR Foundation

After importing AR Foundation into your Unity project, by default you will have enabled the automatic lifecycle management. In order to verify this, open XR Plug-in Management settings by selecting Edit > Project Settings on the top menu bar, and find XR Plug-in Management in the settings sidebar. Once you open it, you will be able to see Initialize XR on Startup toggle. If this is checked, that means you are using automatic lifecycle management.

With automatic lifecycle management, all the AR related subsystems that comes from the build target's provider will be initialized on startup, and deinitialized when the application quits.

Pretia SDK

Pretia SDK has a similar configuration. On the Project Settings window, find Pretia AR Cloud from the sidebar. You will be able to see Initialize on Startup toggle. If this is checked, Pretia related subsystems will also be initialized on startup.

Automatic lifecycle management is a good option to get started quickly, but it's also less efficient in terms of power usage. Even if you are not currently showing the AR session, it will still use up some power for its underlying resources and will drain devices' battery quicker.

Manual Lifecycle Management

AR Foundation

Manually managing the AR lifecycle will keep the batteries of your users device healthier, as it doesn't unnecessarily use resources when not needed. It is also relatively simple to do by calling this API

using UnityEngine.XR.ARFoundation;

// Call before entering a scene with AR session
LoaderUtility.Initialize();

// Call before exiting the AR session
LoaderUtility.Deinitialize();

Pretia SDK

Pretia SDK also provides the same APIs to manually manage the lifecycle.

// Call before entering a scene with Pretia AR components (e.g., ARSharedAnchorManager)
PretiaArCloud.LoaderUtility.Initialize();

// Call before exiting the AR session
PretiaArCloud.LoaderUtility.Deinitialize();

Putting it Together

Both the API calls from AR Foundation and Pretia SDK is required, because each module are managing its own subsystems.

using UnityEngine.XR ARFoundation;

// Initialize subsystems
LoaderUtility.Initialize();
PretiaArCloud.LoaderUtility.Initialize();

// Deinitialize subsystems
LoaderUtility.Deinitialize();
PretiaArCloud.LoaderUtility.Deinitialize();