added generic spawned object loader for images

pull/1/head
Anton Franzluebbers 2024-02-16 16:36:38 -05:00
parent 885b27bfe1
commit 1f9bb4f97f
6 changed files with 113 additions and 16 deletions

View File

@ -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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 16f283d9b4aeffc429940a70600d2e5e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -173,19 +173,15 @@ namespace VELConnect
{ {
if (instance != null) Debug.LogError("VELConnectManager instance already exists", this); if (instance != null) Debug.LogError("VELConnectManager instance already exists", this);
instance = 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(); deviceId = CreateDeviceId();
VelNetManager.OnLocalNetworkObjectSpawned += networkObject =>
{
if (!networkObject.ownershipLocked)
{
// TODO
// SetRoomData("spawned_" + networkObject.networkId, networkObject.prefabName);
}
};
} }
// Computes 15-char device id compatibly with pocketbase // Computes 15-char device id compatibly with pocketbase

View File

@ -45,6 +45,16 @@ namespace VELConnect
{ {
loading = true; loading = true;
if (debugLogs) Debug.Log($"[VelNetPersist] Loading {Id}"); 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 => VELConnectManager.GetDataBlock(Id, data =>
{ {
if (!data.data.TryGetValue("state", out string d)) if (!data.data.TryGetValue("state", out string d))
@ -71,6 +81,16 @@ namespace VELConnect
private void Save() private void Save()
{ {
if (debugLogs) Debug.Log($"[VelNetPersist] Saving {Id}"); 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() VELConnectManager.SetDataBlock(Id, new VELConnectManager.State.DataBlock()
{ {
category = "object_persist", category = "object_persist",

View File

@ -1,7 +1,7 @@
{ {
"name": "edu.uga.engr.vel.vel-connect", "name": "edu.uga.engr.vel.vel-connect",
"displayName": "VEL-Connect", "displayName": "VEL-Connect",
"version": "4.0.1", "version": "4.0.2",
"unity": "2019.1", "unity": "2019.1",
"description": "Web-based configuration for VR applications", "description": "Web-based configuration for VR applications",
"keywords": [], "keywords": [],

View File

@ -36,7 +36,7 @@ export type AuthSystemFields<T = never> = {
// Record types for each collection // Record types for each collection
export type DataBlockRecord<Tdata = unknown> = { export type DataBlockRecord<Tdata = { [key: string]: any }> = {
block_id?: string block_id?: string
category?: string category?: string
data?: null | Tdata data?: null | Tdata
@ -74,10 +74,10 @@ export type UsersRecord = {
} }
// Response types include system fields and match responses from the PocketBase API // Response types include system fields and match responses from the PocketBase API
export type DataBlockResponse<Tdata = unknown, Texpand = unknown> = Required<DataBlockRecord<Tdata>> & BaseSystemFields<Texpand> export type DataBlockResponse<Tdata = { [key: string]: any }, Texpand = unknown> = Required<DataBlockRecord<Tdata>> & BaseSystemFields<Texpand>
export type DeviceResponse<Texpand = unknown> = Required<DeviceRecord> & BaseSystemFields<Texpand> export type DeviceResponse<Texpand = unknown> = Required<DeviceRecord> & BaseSystemFields<Texpand>
export type UserCountResponse<Texpand = unknown> = Required<UserCountRecord> & BaseSystemFields<Texpand> export type UserCountResponse<Texpand = unknown> = Required<UserCountRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand> export type UsersResponse<Texpand = { devices: DeviceResponse[]; profiles: DataBlockResponse[] }> = Required<UsersRecord> & AuthSystemFields<Texpand>
// Types containing all Records and Responses, useful for creating typing helper functions // Types containing all Records and Responses, useful for creating typing helper functions