SpatialAwarenessDemo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class SpatialAwarenessDemo : MonoBehaviour
  8. {
  9. public Text relocStateText;
  10. public Button resaveButton;
  11. public Button saveMapButton;
  12. public SpatialObjectConfig spatialObjectConfig;
  13. public Transform spatialAwarenessRoot;
  14. public float getRelocStateTime = 1f;
  15. private float currentTime = 0f;
  16. private int previousRelocState = 0;
  17. /// <summary>
  18. /// 表示是否正在记忆地图
  19. /// </summary>
  20. private bool rememberingMap = false;
  21. /// <summary>
  22. /// 当识别/丢失地图时触发
  23. /// </summary>
  24. private Action<bool> onAwareness;
  25. List<SpatialGameObjectData> dataList;
  26. // Start is called before the first frame update
  27. private void Start()
  28. {
  29. resaveButton.onClick.AddListener(OnResaveButtonClick);
  30. saveMapButton.onClick.AddListener(OnSaveMapButtonClick);
  31. onAwareness += OnAwareness;
  32. LoadGameObject();
  33. ShowGameObject(false);
  34. }
  35. private void Update()
  36. {
  37. if (rememberingMap) return;
  38. currentTime += Time.deltaTime;
  39. if (currentTime > getRelocStateTime)
  40. {
  41. currentTime = 0f;
  42. int currentRelocState = API_GSXR_Slam.GSXR_Get_OfflineMapRelocState();
  43. relocStateText.text = currentRelocState.ToString();
  44. Debug.Log("currentRelocState:" + currentRelocState);
  45. if (previousRelocState != currentRelocState)
  46. {
  47. onAwareness?.Invoke(currentRelocState == 1);
  48. }
  49. previousRelocState = currentRelocState;
  50. }
  51. if (Input.GetKeyDown(KeyCode.Escape))
  52. {
  53. Application.Quit();
  54. }
  55. }
  56. private void OnDestroy()
  57. {
  58. resaveButton.onClick.RemoveListener(OnResaveButtonClick);
  59. saveMapButton.onClick.RemoveListener(OnSaveMapButtonClick);
  60. onAwareness -= OnAwareness;
  61. }
  62. private void OnResaveButtonClick()
  63. {
  64. rememberingMap = true;
  65. ShowGameObject(true);
  66. ClearGameObject();
  67. LoadGameObject();
  68. API_GSXR_Slam.GSXR_ResaveMap("forTest");
  69. }
  70. private void OnSaveMapButtonClick()
  71. {
  72. rememberingMap = false;
  73. SaveGameObject();
  74. ShowGameObject(false);
  75. previousRelocState = 0;
  76. API_GSXR_Slam.GSXR_SaveMap();
  77. }
  78. private void OnAwareness(bool isAware)
  79. {
  80. ShowGameObject(isAware);
  81. }
  82. private void ShowGameObject(bool showGameObject)
  83. {
  84. Debug.Log("ShowGameObject" + showGameObject);
  85. spatialAwarenessRoot.gameObject.SetActive(showGameObject);
  86. }
  87. private void ClearGameObject()
  88. {
  89. SpatialGameObjectLoader.ClearGameObjectData();
  90. for (int i = 0; i < spatialAwarenessRoot.childCount; i++)
  91. {
  92. Debug.Log("Destroy");
  93. Destroy(spatialAwarenessRoot.GetChild(i).gameObject);
  94. }
  95. }
  96. private void SaveGameObject()
  97. {
  98. if (dataList != null)
  99. {
  100. SpatialGameObjectLoader.SaveGameObjectData(dataList);
  101. }
  102. }
  103. private void LoadGameObject()
  104. {
  105. dataList = SpatialGameObjectLoader.LoadGameObjectData(spatialObjectConfig);
  106. Debug.Log("dataList:" + dataList.Count);
  107. for (int i = 0; i < dataList.Count; i++)
  108. {
  109. Debug.Log("Load");
  110. SpatialGameObjectData spatialGameObjectData = dataList[i];
  111. Vector3 localPosition = Vector3.zero;
  112. Quaternion localRotation = Quaternion.identity;
  113. Vector3 localScale = Vector3.one;
  114. GameObject gameObjectResource = Resources.Load<GameObject>(spatialGameObjectData.GetPrefabName());
  115. spatialGameObjectData.GetTransformData(ref localPosition, ref localRotation, ref localScale);
  116. GameObject gameObjectInstance = GameObject.Instantiate(gameObjectResource, spatialAwarenessRoot);
  117. gameObjectInstance.transform.localPosition = localPosition;
  118. gameObjectInstance.transform.localRotation = localRotation;
  119. gameObjectInstance.transform.localScale = localScale;
  120. gameObjectInstance.GetComponent<SpatialGameObjectMono>().SetDataEntity(spatialGameObjectData);
  121. }
  122. }
  123. }