text
stringlengths
0
1.11k
Next steps
Now that you’ve learned how the Scene Model and Space Setup/Scene Capture works, it’s time to access the data in Unity.
MR Utility Kit provides a rich set of tools and utilities on top of the Scene Model.
OVRSceneManager provides access to the Scene Model via a simple-to-use Unity component that spawns user-defined prefabs for the Scene Anchors in your space.
In order to access the Scene data directly, use the low-level asynchronous OVRAnchor components.
To see how the user’s privacy is protected through permissions, see Spatial Data Permission.
Dive straight into code with our Samples.
To learn about using Scene in practice, see our Best Practices.
Use OVRSceneManager
Unity
All-In-One VR
The platform for which this article is written (Unity) does not match your preferred platform (Nativa).
Haz clic aquí para ver la página del índice de la documentación para tu plataforma de preferencia.
In this page, you will learn how to access the Scene Model through the simple-to-use Unity components OVRSceneManager and OVRSceneAnchor.
What does OVRSceneManager do?
The OVRSceneManager Unity component is the easiest way to access the Scene Model. It focuses on the most common use case: spawning virtual objects that the developer has provided for Scene Anchors of a user’s environment and abstracting away direct Scene data access.
To load an existing Scene Model, call OVRSceneManager.LoadSceneModel(). This call initiates the loading process, which can take multiple frames to complete. As part of the loading process, the Scene Model will instantiate prefabs for all available Scene Anchors. Two events provide the result:
SceneModelLoadedSuccessfully: Indicates that the OVRSceneManager has successfully loaded a Scene Model.
NoSceneModelToLoad: Indicates there is no Scene Model available for the OVRSceneManager to load, for example, if the user has not yet performed Space Setup, or the user denied the Spatial Data Permission
This code sample shows how to subscribe to the events and call LoadSceneModel (note that events should be unsubscribed):
void Load()
{
var sceneManager = FindObjectOfType<OVRSceneManager>();
sceneManager.SceneModelLoadedSuccessfully += OnLoaded;
sceneManager.NoSceneModelToLoad += OnNotLoaded;
sceneManager.LoadSceneModel();
}
void OnLoaded() => Debug.Log("Success");
void OnNotLoaded() => Debug.Log("Failed");
It is also possible to query whether the Scene Model contains a certain anchor classification by calling DoesRoomSetupExist(). This will not load any anchors from Scene Model and therefore not spawn any Unity game objects. This method will always return false if no Scene Model has been captured, or if the user has not granted the runtime permission.
Requesting Space Setup
An app can trigger the system’s space setup process by calling OVRSceneManager.RequestSceneCapture(). Requesting Space Setup will pause the app, launch space setup, and then resume the app when the user completes (or cancels) the setup process. Two events provide the result:
SceneCaptureReturnedWithoutError: Indicates space setup completed. Note that this includes the case where the user cancels space setup.
UnexpectedErrorWithSceneCapture: Indicates something went wrong during space setup.
It is possible to call Space Setup with a list of classification types. This will ensure that the user will have to define these Scene Anchors, and Space Setup cannot be completed until the user has done so.
The following code shows how to call Space Setup with 2 tables and a plant.
void Request()
{
var classifications = new []
{
OVRSceneManager.Classification.Table,
OVRSceneManager.Classification.Table,
OVRSceneManager.Classification.Plant
};
var sceneManager = FindObjectOfType<OVRSceneManager>();
sceneManager.RequestSceneCapture(classifications);
}
Automatically loading Scene Model with OVRSceneModelLoader
To facilitate loading the Scene Model, requesting Space Setup, and handling the results, we have provided an extendable utility component: OVRSceneModelLoader.
The OVRSceneModelLoader component subscribes to the events provided by the OVRSceneManager.RequestSceneCapture and performs an appropriate action in response to each event. It offers reasonable default behaviors, but it is expected that developers create derived classes to override and customize these behaviors.
The default loader checks whether the Scene Model contains any anchors. If not, the loader triggers a Space Setup once. If a user fails to complete Space Setup, then a manual call to RequestSceneCapture is required by the application.
The overridable methods are:
OnStart: The system invokes this method at the start of your application (just after Unity’s Start() method). The default implementation attempts to load an existing Scene model.
OnSceneModelLoadedSuccessfully: This method indicates the Scene Model has finished loading. The default implementation does nothing.
OnNoSceneModelToLoad: This method indicates there was no Scene Model to load, meaning a request to OVRSceneManager.LoadSceneModel concluded, but no captured scene exists or user denied the Spatial Permission. The default implementation requests Space Setup from the OVRSceneManager.
OnSceneCaptureReturnedWithoutError: A request to capture the Scene succeeded without error. The default implementation loads the Scene Model from the OVRSceneManager.
OnUnexpectedErrorWithSceneCapture: A request to capture the Scene failed. The default implementation does nothing.
Using the OVRSceneManager Prefab
The OVRSceneManager and OVRSceneModelLoader components are available through a prefab, also called OVRSceneManager.
Search for the OVRSceneManager prefab in the Project tab, and drag it into your Unity scene.
Create prefabs for the plane and volume prefab.
Drag the created prefabs on the Plane Prefab and Volume Prefab properties of the OVRSceneManager.
If you want to instead spawn objects by classification type, use Prefab Overrides. This option can be used in conjunction with the Plane Prefab and Volume Prefab modes, but takes precendence.
OVRSceneAnchor
The OVRSceneAnchor component represents the Scene Anchors of the Scene Model. It contains multiple components to provide the data, each of which has a duplicate Unity component type (e.g. the Scene Bounded2D component is represented by the OVRScenePlane Unity component).
OVRSceneAnchors are created by the OVRSceneManager and cannot be created by the user. The OVRSceneManager is responsible to periodically updating the location of every anchor through the parameter MaxSceneAnchorUpdatesPerFrame. In order to do this, OVRSceneManager keeps a reference to each spawned OVRSceneAnchor in the scene.
In order to access all of the spawned OVRSceneAnchors, wait until the Scene Model has been loaded, and then use the static helper method OVRSceneAnchor.GetSceneAnchors(). It is recommended to use this method to find specific anchors (such as rooms), instead of using Unity’s FindObjectsOfType<T>().
Further Scene Model Unity components
OVRSceneManager spawns OVRSceneAnchors when the Scene Model has been loaded, and adds the relevant Unity components that provide further data of the Scene Anchor. For example, when spawning a wall, the OVRSceneManager will spawn the supplied Plane Prefab object (with type OVRSceneAnchor) and add the components OVRScenePlane and OVRSemanticClassification if they are not present.
OVRSceneRoom
OVRSceneRoom represents the anchors that are part of the structures that make up a room, providing direct access to the created Unity game objects for the ceiling, floor and walls. All invisible wall faces are also classified as wall faces, and are accessible in the OVRSceneRoom’s walls. The order of the walls is the order in which the user drew them. To access the wall shape as a closed polygon, use the Boundary on the floor’s OVRScenePlane.