Session flow control
During session running, you sometimes need to modify session components. In this case, you need to stop and restart the session. Sometimes you may also need to stop some outputs of the session. This article describes how to control the session running flow.
Before you start
- Learn the basic concepts, composition, and workflow of session through ARSession overview
- Learn how to create session
Session assembly
The assembly process is usually triggered automatically when starting session.
The following code implicitly executes the assembly process.
Session.StartSession();
In some cases, such as when you need 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 and needs to be started through StartCoroutine(IEnumerator).
Start session
AutoStart controls whether session starts automatically. If AutoStart is true (default), session starts automatically at MonoBehaviour.Start().
Session can also be started manually. This requires changing AutoStart to false in advance. Then you can use StartSession() to start session.
Session.StartSession();
Stop session
You can use StopSession(bool) to stop session.
Session.StopSession(keepLastFrame);
The keepLastFrame parameter controls whether to keep the last physical camera image after session stops. This is useful when switching between different sessions and can avoid screen flicker.
Note
keepLastFrame can only control sessions whose image rendering is performed by EasyAR. In general, this parameter is invalid when using AR Foundation or headsets.
Stop session output
When session is running, you can control session output through enabled.
The following code can stop all session output. At this point, session is still running, but no content will be updated, including the physical camera image rendered by EasyAR and the transforms of all nodes controlled by EasyAR.
Session.enabled = false;
Stop session rendering of the physical camera image
You can use ARAssembly.CameraImageRenderer to control rendering of the physical camera image.
The following code can stop 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 first check whether ARAssembly.CameraImageRenderer exists.
Note
ARAssembly.CameraImageRenderer is only valid in sessions whose image rendering is performed by EasyAR. In general, it is invalid when using AR Foundation or headsets. In this case, rendering of the physical camera image is completed by AR Foundation or the headset SDK.
Next steps
- Try accessing AR feature components to learn more control methods for AR features
- Learn how to get session running results
- Learn how to check availability and device support