text
stringlengths 0
1.11k
|
|---|
Save Spatial Anchor UUIDs to PlayerPrefs
|
The OVRSpatialAnchor() methods Save() and Erase() operate on spatial anchors stored in local or in cloud storage. When you want to refer to them, you need their UUIDs. To enable referring to spatial anchors across sessions, you need to save their UUIDs to an external persistent store. This can be any location you choose, though with Unity it is convenient to use PlayerPrefs.
|
In the Starter SamplesSpatial Anchors sample, Anchor.cs includes the SaveToPlayerPrefs() method that saves one or more spatial anchor UUIDs to the Unity PlayerPrefs object. It is invoked after the call to OVRSpatialAnchor.Save() succeeds:
|
void SaveUuidToPlayerPrefs(Guid uuid)
|
{
|
// Write uuid of saved anchor to file
|
if (!PlayerPrefs.HasKey(NumUuidsPlayerPref))
|
{
|
PlayerPrefs.SetInt(NumUuidsPlayerPref, 0);
|
}
|
int playerNumUuids = PlayerPrefs.GetInt(NumUuidsPlayerPref);
|
PlayerPrefs.SetString("uuid" + playerNumUuids, uuid.ToString());
|
PlayerPrefs.SetInt(NumUuidsPlayerPref, ++playerNumUuids);
|
}
|
Find Anchor.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
Load Spatial Anchor UUIDs from PlayerPrefs
|
After you have saved the OVRSpatialAnchor() UUIDs to Unity PlayerPrefs, you retrieve them using the process outlined in Load a Spatial Anchor Stored in the Headset or the Cloud. The source of the spatial anchor UUIDs is PlayerPrefs.
|
In the Starter SamplesSpatial Anchors sample, SpatialAnchorLoader.cs contains methods that implement loading spatial anchor UUIDs from PlayerPrefs that do follow the Load process above. The LoadAnchorsByUuid() method extracts the UUIDs and adds them to the Uuids property of an OVRSpatialAnchor.LoadOptions struct. They are localized and bound later in the same script.
|
public void LoadAnchorsByUuid()
|
{
|
// Get number of saved anchor uuids
|
if (!PlayerPrefs.HasKey(Anchor.NumUuidsPlayerPref))
|
{
|
PlayerPrefs.SetInt(Anchor.NumUuidsPlayerPref, 0);
|
}
|
var playerUuidCount = PlayerPrefs.GetInt(Anchor.NumUuidsPlayerPref);
|
Log($"Attempting to load {playerUuidCount} saved anchors.");
|
if (playerUuidCount == 0)
|
return;
|
var uuids = new Guid[playerUuidCount];
|
for (int i = 0; i < playerUuidCount; ++i)
|
{
|
var uuidKey = "uuid" + i;
|
var currentUuid = PlayerPrefs.GetString(uuidKey);
|
Log("QueryAnchorByUuid: " + currentUuid);
|
uuids[i] = new Guid(currentUuid);
|
}
|
Load(new OVRSpatialAnchor.LoadOptions
|
{
|
Timeout = 0,
|
StorageLocation = OVRSpace.StorageLocation.Local,
|
Uuids = uuids
|
});
|
}
|
Find SpatialAnchorLoader.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
How to handle spatial anchors from another room or floor?
|
As users take their Quest devices to multiple rooms, applications loading spatial anchors from past sessions should be prepared to receive spatial anchors from rooms at a distance, or even from another floor.
|
For example, a ping pong game may save the spatial anchors where ping pong tables have been placed. When the app starts and loads past spatial anchors, it is possible the user is now located in a new room or floor, and the past spatial anchors are therefore far. Restoring ping pong tables on far away spatial anchors leads to a poor user experience – the ping pong tables are small, hard to notice and cannot easily be accessed.
|
Here are best practices to handle these scenarios – these may vary depending on the specific use case of your application.
|
Make it easy for users to position content again – in the ping pong example above, the game should allow users to place a new ping pong table even if one was successfully restored. This makes it easy for users in a new room to set up the game without having to walk back to where they played before.
|
Save spatial anchors for multiple sessions – do not simply keep track of the most recently placed spatial anchor, but instead save and keep track of spatial anchors across multiple sessions. That enables users to use your app in multiple rooms, each with its own set of content.
|
Use the closest spatial anchors when multiple are found – when your app loads spatial anchors from past sessions, multiple of them may be returned and some may be for a distant room. Your app should take a decision on which spatial anchor to use when these are meant to represent the same content. Your app should take into account the distance of the spatial anchor to select which one to use to restore the experience. For example, the ping pong game above can provide a better experience by restoring the table on the closest spatial anchor instead of the most recently used one that may be from another floor.
|
Tips for Using Spatial Anchors
|
For best results:
|
Along with saving anchors to local or cloud storage, you should keep track of anchors you create. If you want to refer to spatial anchors in future sessions, save the spatial anchor UUIDs to an external store. In the Starter Samples Spatial Anchors sample, the Anchor.cs script includes the SaveToPlayerPrefs() method to save a spatial anchor UUID to the Unity PlayerPrefs object. (Find Anchor.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
Don’t bind a spatial anchor to an object that is more than 3 meters away from the spatial anchor. Create a new spatial anchor within 3m of the object instead. Any inaccuracies in pose are amplified the farther away an object is from its spatial anchor.
|
Use parenting to create transform hierarchies between virtual content spaced closely together and their spatial anchor. This can help keep the relative placement of the virtual content consistent.
|
For a large room scene, place an anchor in the middle of the room to make use of its full 3m range.
|
Create new anchors for every new or independent object.
|
Anchors cannot be moved. If the content must be moved, delete the old anchor and create a new one.
|
Destroy and erase any anchors you no longer need or have no content for to improve system performance.
|
There is no maximum to the number of spatial anchors you can create.
|
If your app allows users to place content, allow the user to choose where an object should be located, and then create your spatial anchor at that point.
|
If your app shares spatial anchors with other users (Shared Spatial Anchors), communicate to users that they should walk around their playspace in a large circle prior to using the app to ensure the best experience.
|
If you have saved content + anchor UUID, and the anchor cannot no longer be found, then prompt the user to reposition the content (or auto-reposition it using the scene).Use Shared Spatial Anchors
|
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.
|
Compatibility with SDK Versions Earlier than V57
|
Shared Spatial Anchors are forward compatible, but they are not always backward compatible. Sharing anchors to SDK versions previous to V57 is not guaranteed to work. However, sharing anchors from an older SDK version to a newer SDK version (such as from V52 to V53) will work.
|
The Shared Spatial Anchors (SSA) feature allows players who are located in the same physical space to share content while playing the same game. With SSAs you can create a shared, world-locked frame of reference for many users. For example, two or more people can sit at the same table and play a virtual board game on top of it. Currently, SSA supports local multiplayer games in a single room.
|
Though you create and share spatial anchors with the Insight SDK, you enable the sharing using a third-party network solution. In our documentation and samples for Unity, we make use of Photon Unity Networking. See Multiplayer Enablement for information on best practices and references for creating multiplayer experiences for Meta Quest.
|
The The Big Picture section below describes how SSAs fit into a multiplayer enabled application.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.