/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ using NRKernal.Experimental.Persistence; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.UI; namespace NRKernal.Experimental.NRExamples { /// A local map example. public class LocalMapExample : MonoBehaviour { /// The nr world anchor store. private NRWorldAnchorStore m_NRWorldAnchorStore; /// The image tracking anchor tool. private ImageTrackingAnchorTool m_ImageTrackingAnchorTool; /// The second image tracking anchor tool. public ImageTrackingAnchorTool m_ImageTrackingAnchorTool2; /// The anchor panel. public Transform m_AnchorPanel; /// 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() { m_ImageTrackingAnchorTool = gameObject.GetComponent(); var anchorItems = FindObjectsOfType(); foreach (var item in anchorItems) { item.OnAnchorItemClick += OnAnchorItemClick; m_AnchorPrefabDict.Add(item.key, item.gameObject); } m_AnchorPanel.gameObject.SetActive(false); m_ImageTrackingAnchorTool.OnAnchorLoaded += OnImageTrackingAnchorLoaded; m_ImageTrackingAnchorTool2.OnAnchorLoaded += OnImageTrackingAnchorLoaded; } /// Executes the 'image tracking anchor loaded' action. /// The key. /// The anchor. private void OnImageTrackingAnchorLoaded(string key, NRWorldAnchor anchor) { var go = GameObject.CreatePrimitive(PrimitiveType.Cube); Destroy(go.GetComponent()); go.transform.parent = anchor.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one * 0.3f; go.name = key; } /// 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_AnchorPanel.gameObject.SetActive(!m_AnchorPanel.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(); } /// Create NRWorldAnchorStore object. public void Load() { NRWorldAnchorStore.GetAsync(GetAnchorStoreCallBack); } /// Back, called when the get anchor store. /// The store. private void GetAnchorStoreCallBack(NRWorldAnchorStore store) { if (store == null) { NRDebugger.Info("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; } } } } /// Save anchors your add. public void Save() { if (m_NRWorldAnchorStore == null) { return; } bool result = m_NRWorldAnchorStore.Save(); m_LogStr.AppendLine("Save map result:" + result); } /// Clear all anchors. public void Clear() { if (m_NRWorldAnchorStore == null) { return; } m_NRWorldAnchorStore.Clear(); m_LogStr.AppendLine("Clear map anchor"); } public void Reset() { m_NRWorldAnchorStore?.Reset(); m_LogStr.AppendLine("Reset map"); } /// 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); } } }