SpatialGameObjectLoader.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SpatialGameObjectLoader
  5. {
  6. private const string SPATIAL_GAMEOBJECT_KEY = "SPATIAL_GAMEOBJECT_KEY";
  7. private static List<SpatialGameObjectData> CreateNewGameObjectData(SpatialObjectConfig spatialObjectConfig)
  8. {
  9. List<SpatialGameObjectData> dataList = new List<SpatialGameObjectData>();
  10. float startPlacePositionX = -(spatialObjectConfig.m_PrefabList.Count - 1) * 0.3f / 2f;
  11. for (int i = 0; i < spatialObjectConfig.m_PrefabList.Count; i++)
  12. {
  13. string prefabName = spatialObjectConfig.m_PrefabList[i];
  14. Vector3 localPosition = new Vector3(startPlacePositionX + i * 0.3f, 0, 0);
  15. SpatialGameObjectData spatialGameObject = new SpatialGameObjectData();
  16. spatialGameObject.SetPrefabName(prefabName);
  17. spatialGameObject.SetTransformData(localPosition, Quaternion.identity, Vector3.one * 0.1f);
  18. dataList.Add(spatialGameObject);
  19. }
  20. return dataList;
  21. }
  22. public static List<SpatialGameObjectData> LoadGameObjectData(SpatialObjectConfig spatialObjectConfig)
  23. {
  24. string savedData = PlayerPrefs.GetString(SPATIAL_GAMEOBJECT_KEY, string.Empty);
  25. Debug.Log(savedData);
  26. if (string.IsNullOrEmpty(savedData))
  27. {
  28. return CreateNewGameObjectData(spatialObjectConfig);
  29. }
  30. return JsonUtility.FromJson<SpatialGameObjectDataList>(savedData).data;
  31. }
  32. public static void SaveGameObjectData(List<SpatialGameObjectData> data)
  33. {
  34. SpatialGameObjectDataList dataList = new SpatialGameObjectDataList();
  35. dataList.data = data;
  36. PlayerPrefs.SetString(SPATIAL_GAMEOBJECT_KEY, JsonUtility.ToJson(dataList));
  37. PlayerPrefs.Save();
  38. }
  39. public static void ClearGameObjectData()
  40. {
  41. PlayerPrefs.DeleteKey(SPATIAL_GAMEOBJECT_KEY);
  42. }
  43. }