text
stringlengths
0
1.11k
Set the Rotation property to X = 0, Y = 0, and Z = 0.
Set the Scale property to X = 0.025, Y = 0.025, and Z = 0.025.
In the Inspector, in the Materials property, choose the Green material.
Create the NonSaveablePlacement Prefab
From the Assets menu, select Create > Prefab. Name the new prefab NonSaveablePlacement.
Repeat the steps for Create the SaveablePlacement Prefab, but use the name NonSaveablePlacement for the prefab, and NonSaveableTransform for the new empty object.
Add the Anchor Placement Prefabs to the Left and Right Controller Anchors
The capsule prefabs are displayed when you place the anchor.
In the Hierarchy window, expand OVRCameraRig > TrackingSpace > LeftHandAnchor and select LeftControllerAnchor.
In the Project search box, search for SaveablePlacement. Drag this object to be a child to the RightControllerAnchor.
In the Hierarchy window, expand OVRCameraRig > TrackingSpace > RightHandAnchor and select RightControllerAnchor.
In the Project search box, search for NonSaveablePlacement. Drag this object to be a child to the RightControllerAnchor.
Add the Anchor Prefab
Create an Anchor Manager Script
In the Project pane, click your SpatialAnchors Tutorial folder to make it the current location for new objects.
From the Assets menu, select Create > C# Script. Name the new prefab AnchorTutorialUIManager.
Double-click the new script to edit it.
We want the script just to do a few things:
Respond to button presses:
the left index trigger creates and saves a green spatial anchor.
the left index trigger creates (but does not save) a red spatial anchor.
the X button destroys all displayed spatial anchors.
the A button loads all saved spatial anchors.
the Y button erases all saved spatial anchors.
Keep track of which capsules (green and red) are currently displayed, so we can destroy them.
Keep track of which capsules are saved to local storage (green capsules), to make it easy to load or erase them from local storage.
Separately keep track of the UUIDs of the capsules saved to local storage. We can save these to an external location (such as PlayerPrefs to make it easy to refer to saved capsules in a future session.
Declare the Serialized Objects and Working Variables
We need six serialized fields, one for each for the four prefabs we just made, plus two more for the transforms that were created with the placement prefabs.
At the top of your AnchorTutorialUIManager class, add
[SerializeField]
private GameObject _saveableAnchorPrefab;
public GameObject SaveableAnchorPrefab => _saveableAnchorPrefab;
[SerializeField, FormerlySerializedAs("_saveablePreview")]
private GameObject _saveablePreview;
[SerializeField, FormerlySerializedAs("_saveableTransform")]
private Transform _saveableTransform;
[SerializeField]
private GameObject _nonSaveableAnchorPrefab;
public GameObject NonSaveableAnchorPrefab => _nonSaveableAnchorPrefab;
[SerializeField, FormerlySerializedAs("_nonSaveablePreview")]
private GameObject _nonSaveablePreview;
[SerializeField, FormerlySerializedAs("_nonSaveableTransform")]
private Transform _nonSaveableTransform;
Adding these fields will expose them in the Unity Inspector UI when we add the in the Unity editor later.
We also need a few utility objects to help create the program:
private OVRSpatialAnchor _workingAnchor; // general purpose anchor
private List<OVRSpatialAnchor> _allSavedAnchors; //anchors written these to local storage (green only)
private List<OVRSpatialAnchor> _allRunningAnchors; //anchors currently running (red and green)
private int _anchorSavedUUIDListSize; //current size of the tracking list
private const int _anchorSavedUUIDListMaxSize = 50; //max size of the tracking list
private System.Guid[] _anchorSavedUUIDList; //simulated external location, like PlayerPrefs
Action<OVRSpatialAnchor.UnboundAnchor, bool> _onLoadAnchor; //delegate used for binding unbound anchors
Write the Awake() Method
In Awake(), we instantiate our working variables.
private void Awake(){
//....
//other statements
_allSavedAnchors = new List<OVRSpatialAnchor>();
_allRunningAnchors = new List<OVRSpatialAnchor>();
_anchorSavedUUIDList = new System.Guid[_anchorSavedUUIDListMaxSize];
_anchorSavedUUIDListSize = 0;
_onLoadAnchor = OnLocalized;
//....
//other statements
}
Instantiate a Create Red or Green Capsule
We will use the controller index triggers to create each of the capsules. The process is identical for both capsules: we create a spatial anchor and pass it to the CreateAnchor() method (which we will write in a minute). The only main difference between green and red capsules is that with a saveable green capsule, we pass a true value to the CreateAnchor() method.
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger)) //create a green capsule
{
GameObject gs = PlaceAnchor(_saveableAnchorPrefab, _saveableTransform.position, _saveableTransform.rotation); //anchor A
_workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
CreateAnchor(_workingAnchor, true); //true==save the anchor to local storage
} else if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger)) { // create a red capsule
GameObject gs = PlaceAnchor(_nonSaveableAnchorPrefab, _nonSaveableTransform.position, _nonSaveableTransform.rotation); //anchor b
_workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
CreateAnchor(_workingAnchor, false);
}
The PlaceAnchor() method listed above just instantiates the anchor.
private GameObject PlaceAnchor(GameObject prefab, Vector3 p, Quaternion r)
{