Session flow control
During the operation of a session, sometimes it is necessary to modify session components, which requires stopping and restarting the session. Sometimes it may also be necessary to stop certain outputs of the session. This article describes how to control the flow of a session.
Before you begin
- Learn about the basic concepts, composition, and workflow of a session through Introduction to ARSession
- Learn how to create a session
Session assembly
Usually, the assembly process is automatically triggered when starting a session.
The following code implicitly executes the assembly process:
Session.StartSession();
Sometimes, such as when needing to determine 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 and needs to be started via StartCoroutine(IEnumerator).
Start session
AutoStart controls whether the session starts automatically. If AutoStart is true (the default value), the session will start automatically at 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 controls whether to retain the last frame of the physical camera image after the session stops. This is useful when switching between different sessions to avoid screen flickering.
Note
keepLastFrame can only control sessions that are rendered by EasyAR. Generally, this parameter is invalid when using AR Foundation or a head-mounted display.
Stop session output
While the session is running, you can control the output of the session via enabled.
The following code can stop all outputs of the session. At this time, the session is still running but will not update any content (including the physical camera image rendered by EasyAR and the transforms of all nodes controlled by EasyAR, etc.):
Session.enabled = false;
Stop session from rendering physical camera images
You can use ARAssembly.CameraImageRenderer to control the rendering of the physical camera image.
The following code 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 it is necessary to first check whether ARAssembly.CameraImageRenderer exists.
Note
ARAssembly.CameraImageRenderer is only valid in sessions that are rendered by EasyAR. Generally, it is invalid when using AR Foundation or a head-mounted display, as the physical camera image rendering is handled by AR Foundation or the head-mounted display SDK.
Next steps
- Try accessing AR feature components to learn more about controlling AR features
- Learn how to get the running results of a session
- Learn how to determine availability and device support