diff --git a/unity_package/Runtime/VELConnectPersistenceManager.cs b/unity_package/Runtime/VELConnectPersistenceManager.cs index e539a0f..5d038f3 100644 --- a/unity_package/Runtime/VELConnectPersistenceManager.cs +++ b/unity_package/Runtime/VELConnectPersistenceManager.cs @@ -1,6 +1,4 @@ using System; -using System.Collections; -using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using UnityEngine; @@ -12,14 +10,6 @@ namespace VELConnect { public static VelConnectPersistenceManager instance; - public class SpawnedObjectData - { - public string prefabName; - public string base64ObjectData; - public string networkId; - public int componentIdx; - } - private void Awake() { instance = this; @@ -56,6 +46,7 @@ namespace VELConnect Debug.LogError("Persisted object has no data"); continue; } + NetworkObject spawnedObj = VelNetManager.NetworkInstantiate(persistObject.name, Convert.FromBase64String(persistObject.data)); VelNetPersist persist = spawnedObj.GetComponent(); @@ -63,69 +54,10 @@ namespace VELConnect persist.LoadData(persistObject); } }, s => { Debug.LogError("Failed to get persisted spawned objects", this); }); - - // - // string spawnedObjects = VELConnectManager.GetRoomData("spawned_objects", "[]"); - // List spawnedObjectList = JsonConvert.DeserializeObject>(spawnedObjects); - // List spawnedNetworkObjects = new List(); - // GetSpawnedObjectData(spawnedObjectList, (list) => - // { - // foreach (SpawnedObjectData obj in list) - // { - // NetworkObject spawnedObj = spawnedNetworkObjects.Find(i => i.networkId == obj.networkId); - // if (spawnedObj == null) - // { - // spawnedObj = VelNetManager.NetworkInstantiate(obj.prefabName); - // spawnedNetworkObjects.Add(spawnedObj); - // } - // - // spawnedObj.syncedComponents[obj.componentIdx].ReceiveBytes(Convert.FromBase64String(obj.base64ObjectData)); - // } - // }); } } - private class DataBlocksResponse - { - public List items; - } - - private static void GetSpawnedObjectData(List spawnedObjectList, Action> callback) - { - VELConnectManager.GetRequestCallback($"/api/collections/DataBlock/records?filter=({string.Join(" || ", "id=\"" + spawnedObjectList + "\"")})", (response) => - { - DataBlocksResponse parsedResponse = JsonConvert.DeserializeObject(response); - callback(parsedResponse.items.Select(i => new SpawnedObjectData() - { - networkId = i.block_id.Split("_")[-1], - componentIdx = int.Parse(i.block_id.Split("_").Last()), - prefabName = i.TryGetData("name"), - base64ObjectData = i.TryGetData("state") - }).ToList()); - }); - } - // We don't need to register objects, because they will do that automatically when they spawn if they have the VelNetPersist component - // public static void RegisterObject(NetworkObject obj) - // { - // if (instance == null) - // { - // Debug.LogError("VelConnectPersistenceManager not found in scene"); - // return; - // } - // - // VelNetPersist[] persistedComponents = obj.GetComponents(); - // if (persistedComponents.Length > 1) - // { - // Debug.LogError("NetworkObject has more than one VelNetPersist component"); - // } - // - // foreach (VelNetPersist velNetPersist in persistedComponents) - // { - // velNetPersist.Save(); - // } - // } - // We need to unregister objects when they are destroyed because destroying could happen because we left the scene public static void UnregisterObject(NetworkObject obj) { diff --git a/unity_package/Runtime/VelNetPersist.cs b/unity_package/Runtime/VelNetPersist.cs index 5d2b752..2c56304 100644 --- a/unity_package/Runtime/VelNetPersist.cs +++ b/unity_package/Runtime/VelNetPersist.cs @@ -13,7 +13,7 @@ namespace VELConnect private const float interval = 5f; private double nextUpdate; private bool loading; - private const bool debugLogs = true; + private const bool debugLogs = false; public string persistId; private void Update() @@ -99,20 +99,6 @@ namespace VELConnect using BinaryReader reader = new BinaryReader(new MemoryStream(Convert.FromBase64String(obj.data))); networkObject.UnpackState(reader); - // - // List syncStateComponents = networkObject.syncedComponents.OfType().ToList(); - // if (obj.data.Count != syncStateComponents.Count) - // { - // Debug.LogError($"[VelNetPersist] Different number of components"); - // loading = false; - // return; - // } - // - // for (int i = 0; i < syncStateComponents.Count; i++) - // { - // Debug.Log($"[VelNetPersist] Unpacking {obj.name} {syncStateComponents[i].GetType().Name}"); - // syncStateComponents[i].UnpackState(Convert.FromBase64String(obj.data[i].state)); - // } if (debugLogs) Debug.Log($"[VelNetPersist] Loaded {name}"); loading = false; @@ -131,28 +117,6 @@ namespace VELConnect return; } - // List componentData = new List(); - // foreach (SyncState syncState in syncStateComponents) - // { - // if (syncState == null) - // { - // Debug.LogError("SyncState is null for Persist", this); - // return; - // } - // - // if (syncState.networkObject == null) - // { - // Debug.LogError("Network Object is null for SyncState", syncState); - // return; - // } - // - // componentData.Add(new VELConnectManager.ComponentState() - // { - // componentIdx = networkObject.syncedComponents.IndexOf(syncState), - // state = Convert.ToBase64String(syncState.PackState()) - // }); - // } - using BinaryWriter writer = new BinaryWriter(new MemoryStream()); networkObject.PackState(writer); string data = Convert.ToBase64String(((MemoryStream)writer.BaseStream).ToArray()); @@ -160,7 +124,7 @@ namespace VELConnect // if we have a persistId, update the record, otherwise create a new one if (string.IsNullOrEmpty(persistId)) { - Debug.LogWarning($"We don't have an existing persistId, so we are creating a new record for {networkObject.prefabName}"); + Debug.LogWarning($"We don't have an existing persistId, so we are creating a new record for {networkObject.name}"); VELConnectManager.PostRequestCallback(VELConnectManager.VelConnectUrl + "/api/collections/PersistObject/records", JsonConvert.SerializeObject( new VELConnectManager.PersistObject() { @@ -172,8 +136,6 @@ namespace VELConnect data = data, }), null, s => { - Debug.Log(s); - VELConnectManager.PersistObject resp = JsonConvert.DeserializeObject(s); persistId = resp.id; successCallback?.Invoke(resp); @@ -192,8 +154,6 @@ namespace VELConnect data = data, }), null, s => { - Debug.Log(s); - VELConnectManager.PersistObject resp = JsonConvert.DeserializeObject(s); successCallback?.Invoke(resp); }, method: "PATCH"); @@ -211,8 +171,6 @@ namespace VELConnect VELConnectManager.PostRequestCallback(VELConnectManager.VelConnectUrl + "/api/collections/PersistObject/records/" + persistId, null, null, s => { - Debug.Log(s); - VELConnectManager.PersistObject resp = JsonConvert.DeserializeObject(s); successCallback?.Invoke(resp); }, Debug.LogError, diff --git a/unity_package/package.json b/unity_package/package.json index 9c85d8d..6a91e32 100644 --- a/unity_package/package.json +++ b/unity_package/package.json @@ -1,7 +1,7 @@ { "name": "edu.uga.engr.vel.vel-connect", "displayName": "VEL-Connect", - "version": "4.0.8", + "version": "4.0.9", "unity": "2019.1", "description": "Web-based configuration for VR applications", "keywords": [],