Skip to content

Commit

Permalink
feat: developing a generic Manager class and a DataManager that can s…
Browse files Browse the repository at this point in the history
…ave/load individual managers
  • Loading branch information
dbirman committed Mar 14, 2024
1 parent f8fe8c1 commit 0c8052f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 2 deletions.
66 changes: 66 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs
Original file line number Diff line number Diff line change
@@ -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<Manager> _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;
}
}
11 changes: 11 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/DataManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs
Original file line number Diff line number Diff line change
@@ -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,

}
}
11 changes: 11 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/Managers/Manager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
namespace Urchin.Managers
{

public class PrimitiveMeshManager : MonoBehaviour
public class PrimitiveMeshManager : Manager
{
[SerializeField] private Transform _primitiveMeshParentT;
[SerializeField] private GameObject _primitivePrefabGO;

//Keeping a dictionary mapping names of objects to the game object in schene
private Dictionary<string, MeshBehavior> _meshBehaviors;

#region Properties
public override ManagerType Type { get { return ManagerType.PrimitiveMeshManager; } }
#endregion

#region Unity
private void Awake()
{
_meshBehaviors = new();
Expand All @@ -39,6 +44,8 @@ private void Start()
Client_SocketIO.ClearMeshes += Clear;
}

#endregion

public void UpdateData(MeshModel data)
{
if (_meshBehaviors.ContainsKey(data.id))
Expand Down Expand Up @@ -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<PrimitiveMeshData>(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
}
}
3 changes: 2 additions & 1 deletion UnityClient/Packages/vbl.urchin/Scripts/urchin.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "urchin",
"rootNamespace": "",
"references": [
"GUID:5dedf88781c06a443aeac3e770ba73f9"
"GUID:5dedf88781c06a443aeac3e770ba73f9",
"GUID:91360e4fc352e484eb74c8004f903d9c"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit 0c8052f

Please sign in to comment.