SpatialAwarenessDemo.cs 4.1 KB

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