Session flow control
During the operation of a session, it is sometimes necessary to modify session components, which requires stopping and restarting the session. In some cases, certain outputs of the session may also need to be stopped. This article explains how to control the flow of a session.
Before you begin
- Learn the basic concepts, components, and workflow of a session through Introduction to ARSession
- Learn how to create a session
Assembly of the session
The assembly process is usually triggered automatically when starting the session.
The following code implicitly executes the assembly process.
Session.StartSession();
Sometimes, such as when it is necessary to check availability and device support in advance, you can also use Assemble() to manually trigger the session assembly process:
StartCoroutine(Session.Assemble());
Note
Assemble() returns a coroutine, which needs to be started via StartCoroutine(IEnumerator).
Start session
AutoStart controls whether the session starts automatically. If AutoStart is true (default), the session will start automatically during MonoBehaviour.Start().
The session can also be started manually, which requires setting AutoStart to false in advance. Then, you can use StartSession() to start the session.
Session.StartSession();
Stop session
You can use StopSession(bool) to stop the session.
Session.StopSession(keepLastFrame);
The parameter keepLastFrame can be used to control whether to retain the physical camera image of the last frame after the session stops. This is useful when switching between different sessions to avoid screen flickering.
Note
keepLastFrame only works for sessions where the rendering is handled by EasyAR. Generally, this parameter is ineffective when using AR Foundation or head-mounted displays.
Stop session output
When the session is running, you can control the output of the session through enabled.
The following code can stop all output of the session. At this time, the session is still running, but it will not update anything (including the physical camera image drawn by EasyAR and all node transforms controlled by EasyAR, etc.).
Session.enabled = false;
Stop session from rendering physical camera image
You can use ARAssembly.CameraImageRenderer to control the rendering of the physical camera image.
The following code snippet can stop the rendering of the physical camera image:
if (Session.Assembly != null && Session.Assembly.CameraImageRenderer.OnSome)
{
Session.Assembly.CameraImageRenderer.Value.enabled = false;
}
Note that you need to check whether ARAssembly.CameraImageRenderer exists first.
Note
ARAssembly.CameraImageRenderer is only valid in sessions where the rendering is handled by EasyAR. Generally, it is not effective when using AR Foundation or headset SDKs, as the rendering of the physical camera image is managed by AR Foundation or the headset SDK.
Next steps
- Try accessing AR feature components to learn more about controlling AR features
- Learn how to get session running results
- Learn how to determine availability and device support