92 lines
3.6 KiB
C#
92 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Meta.XR.BuildingBlocks;
|
|
using UnityEngine;
|
|
|
|
namespace Shared
|
|
{
|
|
public class SpatialAnchorManagerScript : MonoBehaviour
|
|
{
|
|
public SpatialAnchorLoaderBuildingBlock loader;
|
|
public GameObject plantStatsDialog;
|
|
public GameObject plantNotesDialog;
|
|
public GameObject plantModel;
|
|
public bool isSupervisor;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
private void Start()
|
|
{
|
|
loader.LoadAnchorsFromDefaultLocalStorage();
|
|
}
|
|
|
|
public void OnCreateAnchorComplete(OVRSpatialAnchor anchor, OVRSpatialAnchor.OperationResult result)
|
|
{
|
|
Debug.Log("Created spatial anchor with ID: " + anchor.Uuid);
|
|
|
|
// get position and rotation of the anchor
|
|
var position = anchor.transform.position;
|
|
var rotation = anchor.transform.rotation;
|
|
Debug.Log($"Anchor Position: {position}, Rotation: {rotation}");
|
|
|
|
// set dialog position and rotation
|
|
var dialogPosition = new Vector3(position.x - 0.2f, position.y + .5f, position.z + 1);
|
|
plantStatsDialog.transform.position = dialogPosition;
|
|
plantStatsDialog.transform.rotation = rotation;
|
|
plantStatsDialog.SetActive(true);
|
|
var plantNotesPosition = new Vector3(position.x + 1, position.y + .5f, position.z + 1);
|
|
plantNotesDialog.transform.position = plantNotesPosition;
|
|
plantNotesDialog.transform.rotation = rotation;
|
|
plantNotesDialog.SetActive(true);
|
|
plantModel.transform.position = position;
|
|
plantModel.transform.rotation = rotation;
|
|
if (isSupervisor)
|
|
{
|
|
plantModel.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void OnLoadAnchorsComplete(List<OVRSpatialAnchor> anchors)
|
|
{
|
|
Debug.Log("Loaded " + anchors.Count + " spatial anchors.");
|
|
|
|
if (anchors.Count <= 0) return;
|
|
|
|
// get position and rotation of the anchor
|
|
var position = anchors.First().transform.position;
|
|
var rotation = anchors.First().transform.rotation;
|
|
Debug.Log($"Anchor Position: {position}, Rotation: {rotation}");
|
|
|
|
// set dialog position and rotation
|
|
var dialogPosition = new Vector3(position.x - 0.2f, position.y + .5f, position.z + 1);
|
|
plantStatsDialog.transform.position = dialogPosition;
|
|
plantStatsDialog.transform.rotation = rotation;
|
|
plantStatsDialog.SetActive(true);
|
|
var plantNotesPosition = new Vector3(position.x + 1, position.y + .5f, position.z + 1);
|
|
plantNotesDialog.transform.position = plantNotesPosition;
|
|
plantNotesDialog.transform.rotation = rotation;
|
|
plantNotesDialog.SetActive(true);
|
|
plantModel.transform.position = position;
|
|
plantModel.transform.rotation = rotation;
|
|
if (isSupervisor)
|
|
{
|
|
plantModel.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void OnEraseAllAnchorsComplete(OVRSpatialAnchor.OperationResult result)
|
|
{
|
|
if (result == OVRSpatialAnchor.OperationResult.Success)
|
|
{
|
|
Debug.Log("All spatial anchors erased successfully.");
|
|
plantStatsDialog.SetActive(false);
|
|
plantNotesDialog.SetActive(false);
|
|
plantModel.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Failed to erase all spatial anchors. Result: " + result);
|
|
}
|
|
}
|
|
}
|
|
} |