From 0c8052f8e9c15d83bb40629f14910b462dae814a Mon Sep 17 00:00:00 2001 From: Dan Birman Date: Thu, 14 Mar 2024 14:27:53 -0700 Subject: [PATCH] feat: developing a generic Manager class and a DataManager that can save/load individual managers --- .../vbl.urchin/Scripts/DataManager.cs | 66 +++++++++++++++++++ .../vbl.urchin/Scripts/DataManager.cs.meta | 11 ++++ .../vbl.urchin/Scripts/Managers/Manager.cs | 18 +++++ .../Scripts/Managers/Manager.cs.meta | 11 ++++ .../Scripts/Managers/PrimitiveMeshManager.cs | 39 ++++++++++- .../Packages/vbl.urchin/Scripts/urchin.asmdef | 3 +- 6 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs.meta create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs.meta diff --git a/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs b/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs new file mode 100644 index 00000000..3b0c36b8 --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs @@ -0,0 +1,66 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Urchin.Managers; + +public class DataManager : MonoBehaviour +{ + [SerializeField] PrimitiveMeshManager _primitiveMeshManager; + + [SerializeField] private string apiURL; + + private List _managers; + + #region Unity + private void Awake() + { + _managers = new(); + _managers.Add(_primitiveMeshManager); + } + #endregion + + public void Save(string sceneName) + { + FlattenedData data = new(); + data.Data = new ManagerData[_managers.Count]; + + for (int i = 0; i < _managers.Count; i++) + { + data.Data[i].Type = _managers[i].Type; + data.Data[i].Data = _managers[i].ToSerializedData(); + } + + // Push data up to the REST API + throw new System.NotImplementedException(); + } + + public void Load(string targetURL) + { + throw new System.NotImplementedException(); + + // Get the manager data back from the API + FlattenedData data = new(); + + foreach (ManagerData managerData in data.Data) + { + switch (managerData.Type) + { + case ManagerType.PrimitiveMeshManager: + _primitiveMeshManager.FromSerializedData(managerData.Data); + break; + } + } + } + + private struct FlattenedData + { + public ManagerData[] Data; + } + + + private struct ManagerData + { + public ManagerType Type; + public string Data; + } +} diff --git a/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs.meta b/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs.meta new file mode 100644 index 00000000..0c239f52 --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68ebc6bd38932624caaa142ffb9d73b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs b/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs new file mode 100644 index 00000000..24d84a5c --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace Urchin.Managers +{ + public abstract class Manager : MonoBehaviour + { + public abstract ManagerType Type { get; } + + public abstract string ToSerializedData(); + public abstract void FromSerializedData(string serializedData); + } + + public enum ManagerType + { + PrimitiveMeshManager = 0, + + } +} \ No newline at end of file diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs.meta b/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs.meta new file mode 100644 index 00000000..438a76fb --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b2102adb770e6ab4b90335eac1bee820 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs b/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs index a667b5f7..21b9fb48 100644 --- a/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs +++ b/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs @@ -8,7 +8,7 @@ namespace Urchin.Managers { - public class PrimitiveMeshManager : MonoBehaviour + public class PrimitiveMeshManager : Manager { [SerializeField] private Transform _primitiveMeshParentT; [SerializeField] private GameObject _primitivePrefabGO; @@ -16,6 +16,11 @@ public class PrimitiveMeshManager : MonoBehaviour //Keeping a dictionary mapping names of objects to the game object in schene private Dictionary _meshBehaviors; + #region Properties + public override ManagerType Type { get { return ManagerType.PrimitiveMeshManager; } } + #endregion + + #region Unity private void Awake() { _meshBehaviors = new(); @@ -39,6 +44,8 @@ private void Start() Client_SocketIO.ClearMeshes += Clear; } + #endregion + public void UpdateData(MeshModel data) { if (_meshBehaviors.ContainsKey(data.id)) @@ -125,5 +132,35 @@ public void SetMaterials(IDListStringList data) _meshBehaviors[data.ids[i]].SetMaterial(data.values[i]); } #endregion + + #region Manager + public override void FromSerializedData(string serializedData) + { + Clear(); + + PrimitiveMeshData data = JsonUtility.FromJson(serializedData); + + foreach (MeshModel modelData in data.Data) + Create(modelData); + } + + public override string ToSerializedData() + { + PrimitiveMeshData data = new(); + data.Data = new MeshModel[_meshBehaviors.Count]; + + MeshBehavior[] meshBehaviors = _meshBehaviors.Values.ToArray(); + + for (int i = 0; i < _meshBehaviors.Count; i++) + data.Data[i] = meshBehaviors[i].Data; + + return JsonUtility.ToJson(data); + } + + private struct PrimitiveMeshData + { + public MeshModel[] Data; + } + #endregion } } \ No newline at end of file diff --git a/UnityClient/Packages/vbl.urchin/Scripts/urchin.asmdef b/UnityClient/Packages/vbl.urchin/Scripts/urchin.asmdef index 34e1b7ec..5cbae7b4 100644 --- a/UnityClient/Packages/vbl.urchin/Scripts/urchin.asmdef +++ b/UnityClient/Packages/vbl.urchin/Scripts/urchin.asmdef @@ -2,7 +2,8 @@ "name": "urchin", "rootNamespace": "", "references": [ - "GUID:5dedf88781c06a443aeac3e770ba73f9" + "GUID:5dedf88781c06a443aeac3e770ba73f9", + "GUID:91360e4fc352e484eb74c8004f903d9c" ], "includePlatforms": [], "excludePlatforms": [],