LocalMapExample.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using NRKernal.Experimental.Persistence;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. namespace NRKernal.Experimental.NRExamples
  15. {
  16. /// <summary> A local map example. </summary>
  17. public class LocalMapExample : MonoBehaviour
  18. {
  19. /// <summary> The nr world anchor store. </summary>
  20. private NRWorldAnchorStore m_NRWorldAnchorStore;
  21. /// <summary> The image tracking anchor tool. </summary>
  22. private ImageTrackingAnchorTool m_ImageTrackingAnchorTool;
  23. /// <summary> The second image tracking anchor tool. </summary>
  24. public ImageTrackingAnchorTool m_ImageTrackingAnchorTool2;
  25. /// <summary> The anchor panel. </summary>
  26. public Transform m_AnchorPanel;
  27. /// <summary> The debug text. </summary>
  28. public Text debugText;
  29. /// <summary> Target for the. </summary>
  30. private Transform target;
  31. /// <summary> Dictionary of anchor prefabs. </summary>
  32. private Dictionary<string, GameObject> m_AnchorPrefabDict = new Dictionary<string, GameObject>();
  33. /// <summary> Dictionary of loaded anchors. </summary>
  34. private Dictionary<string, GameObject> m_LoadedAnchorDict = new Dictionary<string, GameObject>();
  35. /// <summary> The log string. </summary>
  36. private StringBuilder m_LogStr = new StringBuilder();
  37. /// <summary> Starts this object. </summary>
  38. private void Start()
  39. {
  40. m_ImageTrackingAnchorTool = gameObject.GetComponent<ImageTrackingAnchorTool>();
  41. var anchorItems = FindObjectsOfType<AnchorItem>();
  42. foreach (var item in anchorItems)
  43. {
  44. item.OnAnchorItemClick += OnAnchorItemClick;
  45. m_AnchorPrefabDict.Add(item.key, item.gameObject);
  46. }
  47. m_AnchorPanel.gameObject.SetActive(false);
  48. m_ImageTrackingAnchorTool.OnAnchorLoaded += OnImageTrackingAnchorLoaded;
  49. m_ImageTrackingAnchorTool2.OnAnchorLoaded += OnImageTrackingAnchorLoaded;
  50. }
  51. /// <summary> Executes the 'image tracking anchor loaded' action. </summary>
  52. /// <param name="key"> The key.</param>
  53. /// <param name="anchor"> The anchor.</param>
  54. private void OnImageTrackingAnchorLoaded(string key, NRWorldAnchor anchor)
  55. {
  56. var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
  57. Destroy(go.GetComponent<BoxCollider>());
  58. go.transform.parent = anchor.transform;
  59. go.transform.localPosition = Vector3.zero;
  60. go.transform.localRotation = Quaternion.identity;
  61. go.transform.localScale = Vector3.one * 0.3f;
  62. go.name = key;
  63. }
  64. /// <summary> Updates this object. </summary>
  65. private void Update()
  66. {
  67. if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
  68. {
  69. AddAnchor();
  70. }
  71. debugText.text = m_LogStr.ToString();
  72. }
  73. /// <summary> Open or close anchor panel. </summary>
  74. public void SwitchAnchorPanel()
  75. {
  76. m_AnchorPanel.gameObject.SetActive(!m_AnchorPanel.gameObject.activeInHierarchy);
  77. }
  78. /// <summary> Executes the 'anchor item click' action. </summary>
  79. /// <param name="key"> The key.</param>
  80. /// <param name="anchorItem"> The anchor item.</param>
  81. private void OnAnchorItemClick(string key, GameObject anchorItem)
  82. {
  83. if (target != null)
  84. {
  85. DestroyImmediate(target.gameObject);
  86. }
  87. target = Instantiate(anchorItem).transform;
  88. target.parent = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.RightModelAnchor);
  89. target.position = target.parent.transform.position + target.parent.forward;
  90. target.forward = target.parent.forward;
  91. Destroy(target.gameObject.GetComponent<BoxCollider>());
  92. this.SwitchAnchorPanel();
  93. }
  94. /// <summary> Create NRWorldAnchorStore object. </summary>
  95. public void Load()
  96. {
  97. NRWorldAnchorStore.GetAsync(GetAnchorStoreCallBack);
  98. }
  99. /// <summary> Back, called when the get anchor store. </summary>
  100. /// <param name="store"> The store.</param>
  101. private void GetAnchorStoreCallBack(NRWorldAnchorStore store)
  102. {
  103. if (store == null)
  104. {
  105. NRDebugger.Info("store is null");
  106. return;
  107. }
  108. m_NRWorldAnchorStore = store;
  109. m_LogStr.AppendLine("Load map result: true");
  110. var keys = m_NRWorldAnchorStore.GetAllIds();
  111. if (keys != null)
  112. {
  113. foreach (var key in m_NRWorldAnchorStore.GetAllIds())
  114. {
  115. m_LogStr.AppendLine("Get a anchor from NRWorldAnchorStore key: " + key);
  116. GameObject prefab;
  117. if (m_AnchorPrefabDict.TryGetValue(key, out prefab))
  118. {
  119. var go = Instantiate(prefab);
  120. m_NRWorldAnchorStore.Load(key, go);
  121. m_LoadedAnchorDict[key] = go;
  122. }
  123. }
  124. }
  125. }
  126. /// <summary> Save anchors your add. </summary>
  127. public void Save()
  128. {
  129. if (m_NRWorldAnchorStore == null)
  130. {
  131. return;
  132. }
  133. bool result = m_NRWorldAnchorStore.Save();
  134. m_LogStr.AppendLine("Save map result:" + result);
  135. }
  136. /// <summary> Clear all anchors. </summary>
  137. public void Clear()
  138. {
  139. if (m_NRWorldAnchorStore == null)
  140. {
  141. return;
  142. }
  143. m_NRWorldAnchorStore.Clear();
  144. m_LogStr.AppendLine("Clear map anchor");
  145. }
  146. public void Reset()
  147. {
  148. m_NRWorldAnchorStore?.Reset();
  149. m_LogStr.AppendLine("Reset map");
  150. }
  151. /// <summary> Add a new anchor. </summary>
  152. public void AddAnchor()
  153. {
  154. if (m_NRWorldAnchorStore == null || target == null)
  155. {
  156. return;
  157. }
  158. var anchorItem = target.GetComponent<AnchorItem>();
  159. if (anchorItem == null)
  160. {
  161. return;
  162. }
  163. var go = Instantiate(target.gameObject);
  164. go.transform.position = target.position;
  165. go.transform.rotation = target.rotation;
  166. go.SetActive(true);
  167. string key = go.GetComponent<AnchorItem>().key;
  168. m_NRWorldAnchorStore.Delete(key);
  169. bool result = m_NRWorldAnchorStore.AddAnchor(key, go);
  170. if (!result)
  171. {
  172. DestroyImmediate(go);
  173. }
  174. else
  175. {
  176. GameObject lastgo;
  177. m_LoadedAnchorDict.TryGetValue(key, out lastgo);
  178. if (lastgo != null)
  179. {
  180. DestroyImmediate(lastgo);
  181. }
  182. m_LoadedAnchorDict[key] = go;
  183. }
  184. DestroyImmediate(target.gameObject);
  185. m_LogStr.AppendLine("Add anchor " + result);
  186. }
  187. }
  188. }