AnchorLoadTool.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. namespace NRKernal.Experimental.Persistence
  10. {
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. /// <summary> A local map example. </summary>
  16. public class AnchorLoadTool : MonoBehaviour
  17. {
  18. /// <summary> The nr world anchor store. </summary>
  19. private NRWorldAnchorStore m_NRWorldAnchorStore;
  20. /// <summary> The anchor panel. </summary>
  21. public Transform m_AnchorsHolder;
  22. /// <summary> The debug text. </summary>
  23. public Text debugText;
  24. /// <summary> Target for the. </summary>
  25. private Transform target;
  26. /// <summary> Dictionary of anchor prefabs. </summary>
  27. private Dictionary<string, GameObject> m_AnchorPrefabDict = new Dictionary<string, GameObject>();
  28. /// <summary> Dictionary of loaded anchors. </summary>
  29. private Dictionary<string, GameObject> m_LoadedAnchorDict = new Dictionary<string, GameObject>();
  30. /// <summary> The log string. </summary>
  31. private StringBuilder m_LogStr = new StringBuilder();
  32. /// <summary> Starts this object. </summary>
  33. private void Start()
  34. {
  35. var anchorItems = FindObjectsOfType<AnchorItem>();
  36. foreach (var item in anchorItems)
  37. {
  38. item.OnAnchorItemClick += OnAnchorItemClick;
  39. m_AnchorPrefabDict.Add(item.key, item.gameObject);
  40. }
  41. m_AnchorsHolder.gameObject.SetActive(false);
  42. NRWorldAnchorStore.GetAsync(GetAnchorStoreCallBack);
  43. }
  44. /// <summary> Updates this object. </summary>
  45. private void Update()
  46. {
  47. if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
  48. {
  49. AddAnchor();
  50. }
  51. debugText.text = m_LogStr.ToString();
  52. }
  53. /// <summary> Open or close anchor panel. </summary>
  54. public void SwitchAnchorPanel()
  55. {
  56. m_AnchorsHolder.gameObject.SetActive(!m_AnchorsHolder.gameObject.activeInHierarchy);
  57. }
  58. /// <summary> Executes the 'anchor item click' action. </summary>
  59. /// <param name="key"> The key.</param>
  60. /// <param name="anchorItem"> The anchor item.</param>
  61. private void OnAnchorItemClick(string key, GameObject anchorItem)
  62. {
  63. if (target != null)
  64. {
  65. DestroyImmediate(target.gameObject);
  66. }
  67. target = Instantiate(anchorItem).transform;
  68. target.parent = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.RightModelAnchor);
  69. target.position = target.parent.transform.position + target.parent.forward;
  70. target.forward = target.parent.forward;
  71. Destroy(target.gameObject.GetComponent<BoxCollider>());
  72. this.SwitchAnchorPanel();
  73. }
  74. /// <summary> Back, called when the get anchor store. </summary>
  75. /// <param name="store"> The store.</param>
  76. private void GetAnchorStoreCallBack(NRWorldAnchorStore store)
  77. {
  78. if (store == null)
  79. {
  80. NRDebugger.Warning("[AnchorLoadTool] Store is null");
  81. return;
  82. }
  83. m_NRWorldAnchorStore = store;
  84. m_LogStr.AppendLine("Load map result: true");
  85. var keys = m_NRWorldAnchorStore.GetAllIds();
  86. if (keys != null)
  87. {
  88. foreach (var key in m_NRWorldAnchorStore.GetAllIds())
  89. {
  90. m_LogStr.AppendLine("Get a anchor from NRWorldAnchorStore key: " + key);
  91. GameObject prefab;
  92. if (m_AnchorPrefabDict.TryGetValue(key, out prefab))
  93. {
  94. var go = Instantiate(prefab);
  95. m_NRWorldAnchorStore.Load(key, go);
  96. m_LoadedAnchorDict[key] = go;
  97. }
  98. }
  99. }
  100. }
  101. /// <summary> Clear all anchors. </summary>
  102. public void Clear()
  103. {
  104. if (m_NRWorldAnchorStore == null)
  105. {
  106. return;
  107. }
  108. m_NRWorldAnchorStore.Clear();
  109. m_LogStr.AppendLine("Clear all anchors");
  110. }
  111. /// <summary> Add a new anchor. </summary>
  112. public void AddAnchor()
  113. {
  114. if (m_NRWorldAnchorStore == null || target == null)
  115. {
  116. return;
  117. }
  118. var anchorItem = target.GetComponent<AnchorItem>();
  119. if (anchorItem == null)
  120. {
  121. return;
  122. }
  123. var go = Instantiate(target.gameObject);
  124. go.transform.position = target.position;
  125. go.transform.rotation = target.rotation;
  126. go.SetActive(true);
  127. string key = go.GetComponent<AnchorItem>().key;
  128. m_NRWorldAnchorStore.Delete(key);
  129. bool result = m_NRWorldAnchorStore.AddAnchor(key, go);
  130. if (!result)
  131. {
  132. DestroyImmediate(go);
  133. }
  134. else
  135. {
  136. GameObject lastgo;
  137. m_LoadedAnchorDict.TryGetValue(key, out lastgo);
  138. if (lastgo != null)
  139. {
  140. DestroyImmediate(lastgo);
  141. }
  142. m_LoadedAnchorDict[key] = go;
  143. }
  144. DestroyImmediate(target.gameObject);
  145. m_LogStr.AppendLine("Add anchor " + result);
  146. }
  147. }
  148. }