Skip to main content
Version: 31 Aug 2023

Spatial Anchors Examples

This section contains code that can be used to demo Magic Leap 2's Spatial Anchors functionality. It can also be used as a reference when creating Applications that take advantage of the Anchors API.

Spatial Anchor Visualizer Minimal Example

This example script allows users that are localized into a map to do the following:

  • Visualize existing anchors
  • Create anchors by pressing the trigger
  • Delete them by pressing the bumper

This feature requires the SPATIAL_ANCHORS permission to be enabled in your project's Manifest Settings. (Edit > Project Settings > Magic Leap > Manifest Settings)

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.MagicLeap;

public class AnchorExampleScript : MonoBehaviour
//How many meters from the origin should the anchor request search?
public float SearchRadius = 10;

private MagicLeapInputs _magicLeapInputs;
private MagicLeapInputs.ControllerActions _controllerActions;

private MLAnchors.Request _mlAnchorsRequest;
private MLAnchors.Request.Params _anchorRequestParams;

private Dictionary<string, GameObject> _gameObjectsByAnchorId = new Dictionary<string, GameObject>();

void Start()
// Initialize controller input events.
_magicLeapInputs = new MagicLeapInputs();

_controllerActions = new MagicLeapInputs.ControllerActions(_magicLeapInputs);
_controllerActions.Bumper.started += HandleOnBumper;
_controllerActions.Trigger.started += HandleOnTrigger;

//Initialize Magic leap Spatial Anchor Requests
_mlAnchorsRequest = new MLAnchors.Request();

//Get the user's current Localization Info and debugs it
MLResult mlResult = MLAnchors.GetLocalizationInfo(out MLAnchors.LocalizationInfo info);
Debug.Log("Localization Info " + info);

void Update()
// Setting Max result or radius to zero means the search will not be constrained.
Vector3 controllerPosition = _controllerActions.Position.ReadValue<Vector3>();
_anchorRequestParams = new MLAnchors.Request.Params(controllerPosition, SearchRadius, 0,false);

// Starts the search using the parameters above.
MLResult startStatus = _mlAnchorsRequest.Start(_anchorRequestParams);
if (!startStatus.IsOk)
Debug.Log("Could not start Request : " + startStatus);

// Query the search for anchors.
MLResult resultStatus = _mlAnchorsRequest.TryGetResult(out MLAnchors.Request.Result result);
if (!resultStatus.IsOk)
Debug.Log("Could not get Anchors : " + resultStatus);

// Iterate through the anchors found in the search.
for (int i = 0; i < result.anchors.Length; ++i)
MLAnchors.Anchor anchor = result.anchors[i];
string id = anchor.Id;

// If the anchor was already created, update it's position and rotation.
if (_gameObjectsByAnchorId.ContainsKey(id))
GameObject anchorVisual = _gameObjectsByAnchorId[id];
anchorVisual.transform.position = anchor.Pose.position;
anchorVisual.transform.rotation = anchor.Pose.rotation;
// If we located a new anchor, create a cube at it's location and store it in our dictionary.
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//Render the cube with the default URP shader
cube.GetComponent<Renderer>().material = new Material(Shader.Find("Universal Render Pipeline/Lit"));

//Scale the cube so it is a reasonable size
cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);

cube.transform.position = anchor.Pose.position;
cube.transform.rotation = anchor.Pose.rotation;
_gameObjectsByAnchorId.Add(id, cube);

// Delete the closest anchor to the controller when the user presses the bumper.
private void HandleOnBumper(InputAction.CallbackContext obj)
// Start the search using the parameters specified in the Update function.
// Get the search results.
_mlAnchorsRequest.TryGetResult(out MLAnchors.Request.Result result);
if (result.anchors.Length > 0)
// Get the closest anchor.
var anchor = result.anchors[0];
//Delete the anchor using the Anchor's ID
//Delete the gameObject if it exists
if (_gameObjectsByAnchorId.ContainsKey(anchor.Id))
GameObject anchorVisual = _gameObjectsByAnchorId[anchor.Id];

//Create a new anchor at the controllers location when the user presses the trigger.
private void HandleOnTrigger(InputAction.CallbackContext obj)
// Create a pose from the controller's position and rotation.
Pose controllerPose = new Pose(_controllerActions.Position.ReadValue<Vector3>(),
// Create a new anchor at the location of the controller.
MLAnchors.Anchor.Create(controllerPose, 300, out MLAnchors.Anchor anchor);
// Publish the anchor to the map after it is created.

private void OnDestroy()
_controllerActions.Bumper.started -= HandleOnBumper;
_controllerActions.Trigger.started -= HandleOnTrigger;

Spatial Anchors Full Example Project