ContentStorageManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using System.IO;
  11. namespace Immersal.Samples.ContentPlacement
  12. {
  13. public class ContentStorageManager : MonoBehaviour
  14. {
  15. [HideInInspector]
  16. public List<MovableContent> contentList = new List<MovableContent>();
  17. [SerializeField]
  18. private GameObject m_ContentPrefab = null;
  19. [SerializeField]
  20. private Immersal.AR.ARSpace m_ARSpace;
  21. [SerializeField]
  22. private string m_Filename = "content.json";
  23. private Savefile m_Savefile;
  24. private List<Vector3> m_Positions = new List<Vector3>();
  25. [System.Serializable]
  26. public struct Savefile
  27. {
  28. public List<Vector3> positions;
  29. }
  30. public static ContentStorageManager Instance
  31. {
  32. get
  33. {
  34. #if UNITY_EDITOR
  35. if (instance == null && !Application.isPlaying)
  36. {
  37. instance = UnityEngine.Object.FindObjectOfType<ContentStorageManager>();
  38. }
  39. #endif
  40. if (instance == null)
  41. {
  42. Debug.LogError("No ContentStorageManager instance found. Ensure one exists in the scene.");
  43. }
  44. return instance;
  45. }
  46. }
  47. private static ContentStorageManager instance = null;
  48. void Awake()
  49. {
  50. if (instance == null)
  51. {
  52. instance = this;
  53. }
  54. if (instance != this)
  55. {
  56. Debug.LogError("There must be only one ContentStorageManager object in a scene.");
  57. UnityEngine.Object.DestroyImmediate(this);
  58. return;
  59. }
  60. if (m_ARSpace == null)
  61. {
  62. m_ARSpace = GameObject.FindObjectOfType<Immersal.AR.ARSpace>();
  63. }
  64. }
  65. private void Start()
  66. {
  67. contentList.Clear();
  68. LoadContents();
  69. }
  70. public void AddContent()
  71. {
  72. Transform cameraTransform = Camera.main.transform;
  73. GameObject go = Instantiate(m_ContentPrefab, cameraTransform.position + cameraTransform.forward, Quaternion.identity, m_ARSpace.transform);
  74. }
  75. public void DeleteAllContent()
  76. {
  77. List<MovableContent> copy = new List<MovableContent>();
  78. foreach (MovableContent content in contentList)
  79. {
  80. copy.Add(content);
  81. }
  82. foreach(MovableContent content in copy)
  83. {
  84. content.RemoveContent();
  85. }
  86. }
  87. public void SaveContents()
  88. {
  89. m_Positions.Clear();
  90. foreach (MovableContent content in contentList)
  91. {
  92. m_Positions.Add(content.transform.localPosition);
  93. }
  94. m_Savefile.positions = m_Positions;
  95. string jsonstring = JsonUtility.ToJson(m_Savefile, true);
  96. string dataPath = Path.Combine(Application.persistentDataPath, m_Filename);
  97. File.WriteAllText(dataPath, jsonstring);
  98. }
  99. public void LoadContents()
  100. {
  101. string dataPath = Path.Combine(Application.persistentDataPath, m_Filename);
  102. Debug.Log(string.Format("Trying to load file: {0}", dataPath));
  103. try
  104. {
  105. Savefile loadFile = JsonUtility.FromJson<Savefile>(File.ReadAllText(dataPath));
  106. foreach (Vector3 pos in loadFile.positions)
  107. {
  108. GameObject go = Instantiate(m_ContentPrefab, m_ARSpace.transform);
  109. go.transform.localPosition = pos;
  110. }
  111. Debug.Log("Successfully loaded file!");
  112. }
  113. catch (FileNotFoundException e)
  114. {
  115. Debug.Log(e.Message + "\n.json file for content storage not found. Created a new file!");
  116. File.WriteAllText(dataPath, "");
  117. }
  118. }
  119. }
  120. }