/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental.Persistence
{
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
/// A local map example.
public class AnchorLoadTool : MonoBehaviour
{
/// The nr world anchor store.
private NRWorldAnchorStore m_NRWorldAnchorStore;
/// The anchor panel.
public Transform m_AnchorsHolder;
/// The debug text.
public Text debugText;
/// Target for the.
private Transform target;
/// Dictionary of anchor prefabs.
private Dictionary m_AnchorPrefabDict = new Dictionary();
/// Dictionary of loaded anchors.
private Dictionary m_LoadedAnchorDict = new Dictionary();
/// The log string.
private StringBuilder m_LogStr = new StringBuilder();
/// Starts this object.
private void Start()
{
var anchorItems = FindObjectsOfType();
foreach (var item in anchorItems)
{
item.OnAnchorItemClick += OnAnchorItemClick;
m_AnchorPrefabDict.Add(item.key, item.gameObject);
}
m_AnchorsHolder.gameObject.SetActive(false);
NRWorldAnchorStore.GetAsync(GetAnchorStoreCallBack);
}
/// Updates this object.
private void Update()
{
if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
{
AddAnchor();
}
debugText.text = m_LogStr.ToString();
}
/// Open or close anchor panel.
public void SwitchAnchorPanel()
{
m_AnchorsHolder.gameObject.SetActive(!m_AnchorsHolder.gameObject.activeInHierarchy);
}
/// Executes the 'anchor item click' action.
/// The key.
/// The anchor item.
private void OnAnchorItemClick(string key, GameObject anchorItem)
{
if (target != null)
{
DestroyImmediate(target.gameObject);
}
target = Instantiate(anchorItem).transform;
target.parent = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.RightModelAnchor);
target.position = target.parent.transform.position + target.parent.forward;
target.forward = target.parent.forward;
Destroy(target.gameObject.GetComponent());
this.SwitchAnchorPanel();
}
/// Back, called when the get anchor store.
/// The store.
private void GetAnchorStoreCallBack(NRWorldAnchorStore store)
{
if (store == null)
{
NRDebugger.Warning("[AnchorLoadTool] Store is null");
return;
}
m_NRWorldAnchorStore = store;
m_LogStr.AppendLine("Load map result: true");
var keys = m_NRWorldAnchorStore.GetAllIds();
if (keys != null)
{
foreach (var key in m_NRWorldAnchorStore.GetAllIds())
{
m_LogStr.AppendLine("Get a anchor from NRWorldAnchorStore key: " + key);
GameObject prefab;
if (m_AnchorPrefabDict.TryGetValue(key, out prefab))
{
var go = Instantiate(prefab);
m_NRWorldAnchorStore.Load(key, go);
m_LoadedAnchorDict[key] = go;
}
}
}
}
/// Clear all anchors.
public void Clear()
{
if (m_NRWorldAnchorStore == null)
{
return;
}
m_NRWorldAnchorStore.Clear();
m_LogStr.AppendLine("Clear all anchors");
}
/// Add a new anchor.
public void AddAnchor()
{
if (m_NRWorldAnchorStore == null || target == null)
{
return;
}
var anchorItem = target.GetComponent();
if (anchorItem == null)
{
return;
}
var go = Instantiate(target.gameObject);
go.transform.position = target.position;
go.transform.rotation = target.rotation;
go.SetActive(true);
string key = go.GetComponent().key;
m_NRWorldAnchorStore.Delete(key);
bool result = m_NRWorldAnchorStore.AddAnchor(key, go);
if (!result)
{
DestroyImmediate(go);
}
else
{
GameObject lastgo;
m_LoadedAnchorDict.TryGetValue(key, out lastgo);
if (lastgo != null)
{
DestroyImmediate(lastgo);
}
m_LoadedAnchorDict[key] = go;
}
DestroyImmediate(target.gameObject);
m_LogStr.AppendLine("Add anchor " + result);
}
}
}