From 1f9bb4f97fc44e846bf879aba5423bda32ad1f9c Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Fri, 16 Feb 2024 16:36:38 -0500 Subject: [PATCH] added generic spawned object loader for images --- unity_package/Runtime/GenericSpawnedObject.cs | 70 +++++++++++++++++++ .../Runtime/GenericSpawnedObject.cs.meta | 11 +++ unity_package/Runtime/VELConnectManager.cs | 20 +++--- unity_package/Runtime/VelNetPersist.cs | 20 ++++++ unity_package/package.json | 2 +- velconnect-npm/src/types/pocketbase-types.ts | 6 +- 6 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 unity_package/Runtime/GenericSpawnedObject.cs create mode 100644 unity_package/Runtime/GenericSpawnedObject.cs.meta diff --git a/unity_package/Runtime/GenericSpawnedObject.cs b/unity_package/Runtime/GenericSpawnedObject.cs new file mode 100644 index 0000000..72c0f69 --- /dev/null +++ b/unity_package/Runtime/GenericSpawnedObject.cs @@ -0,0 +1,70 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; +using UnityEngine.Networking; +using UnityEngine.UI; +using VelNet; + +namespace VELConnect +{ + public class GenericSpawnedObject : SyncState + { + private string url; + + private string Url + { + get => url; + set + { + if (url == value) return; + + url = value; + + if (url.EndsWith(".png") || url.EndsWith(".jpg")) + { + StartCoroutine(DownloadImage(url)); + } + else + { + Debug.LogError("Invalid image url: " + url); + } + } + } + + public RawImage rawImage; + + public void Init(string dataUrl) + { + Url = dataUrl; + } + + protected override void SendState(BinaryWriter binaryWriter) + { + binaryWriter.Write(Url); + } + + protected override void ReceiveState(BinaryReader binaryReader) + { + Url = binaryReader.ReadString(); + } + + private IEnumerator DownloadImage(string downloadUrl) + { + UnityWebRequest request = UnityWebRequestTexture.GetTexture(downloadUrl); + yield return request.SendWebRequest(); + if (request.result != UnityWebRequest.Result.Success) + { + Debug.Log(request.error); + yield break; + } + + rawImage.texture = ((DownloadHandlerTexture)request.downloadHandler).texture; + float aspect = (float)rawImage.texture.width / rawImage.texture.height; + Transform t = transform; + Vector3 s = t.localScale; + s = new Vector3(aspect * s.y, s.y, s.z); + t.localScale = s; + } + } +} \ No newline at end of file diff --git a/unity_package/Runtime/GenericSpawnedObject.cs.meta b/unity_package/Runtime/GenericSpawnedObject.cs.meta new file mode 100644 index 0000000..dd44ce1 --- /dev/null +++ b/unity_package/Runtime/GenericSpawnedObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 16f283d9b4aeffc429940a70600d2e5e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity_package/Runtime/VELConnectManager.cs b/unity_package/Runtime/VELConnectManager.cs index 1feae4d..c91a17f 100644 --- a/unity_package/Runtime/VELConnectManager.cs +++ b/unity_package/Runtime/VELConnectManager.cs @@ -173,19 +173,15 @@ namespace VELConnect { if (instance != null) Debug.LogError("VELConnectManager instance already exists", this); instance = this; - - // Compute device id - MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); - StringBuilder sb = new StringBuilder(SystemInfo.deviceUniqueIdentifier); - sb.Append(Application.productName); -#if UNITY_EDITOR - // allows running multiple builds on the same computer - // return SystemInfo.deviceUniqueIdentifier + Hash128.Compute(Application.dataPath); - sb.Append(Application.dataPath); - sb.Append("EDITOR"); -#endif - string id = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))); deviceId = CreateDeviceId(); + VelNetManager.OnLocalNetworkObjectSpawned += networkObject => + { + if (!networkObject.ownershipLocked) + { + // TODO + // SetRoomData("spawned_" + networkObject.networkId, networkObject.prefabName); + } + }; } // Computes 15-char device id compatibly with pocketbase diff --git a/unity_package/Runtime/VelNetPersist.cs b/unity_package/Runtime/VelNetPersist.cs index e8df8ef..809d13c 100644 --- a/unity_package/Runtime/VelNetPersist.cs +++ b/unity_package/Runtime/VelNetPersist.cs @@ -45,6 +45,16 @@ namespace VELConnect { loading = true; if (debugLogs) Debug.Log($"[VelNetPersist] Loading {Id}"); + 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; + } VELConnectManager.GetDataBlock(Id, data => { if (!data.data.TryGetValue("state", out string d)) @@ -71,6 +81,16 @@ namespace VELConnect private void Save() { if (debugLogs) Debug.Log($"[VelNetPersist] Saving {Id}"); + 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; + } VELConnectManager.SetDataBlock(Id, new VELConnectManager.State.DataBlock() { category = "object_persist", diff --git a/unity_package/package.json b/unity_package/package.json index 2bdd01b..0ce57c4 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.1", + "version": "4.0.2", "unity": "2019.1", "description": "Web-based configuration for VR applications", "keywords": [], diff --git a/velconnect-npm/src/types/pocketbase-types.ts b/velconnect-npm/src/types/pocketbase-types.ts index 9499f9a..a176947 100644 --- a/velconnect-npm/src/types/pocketbase-types.ts +++ b/velconnect-npm/src/types/pocketbase-types.ts @@ -36,7 +36,7 @@ export type AuthSystemFields = { // Record types for each collection -export type DataBlockRecord = { +export type DataBlockRecord = { block_id?: string category?: string data?: null | Tdata @@ -74,10 +74,10 @@ export type UsersRecord = { } // Response types include system fields and match responses from the PocketBase API -export type DataBlockResponse = Required> & BaseSystemFields +export type DataBlockResponse = Required> & BaseSystemFields export type DeviceResponse = Required & BaseSystemFields export type UserCountResponse = Required & BaseSystemFields -export type UsersResponse = Required & AuthSystemFields +export type UsersResponse = Required & AuthSystemFields // Types containing all Records and Responses, useful for creating typing helper functions