async Task DownloadSyncObjectInstance(StreamInstance streamInstance, CancellationToken token) { DownloadResult downloadResult; Task <DownloadResult> task; lock (m_Tasks) { m_Tasks.TryGetValue(streamInstance.instance.ObjectId, out task); } if (task != null && !task.IsCompleted) { var result = await task; downloadResult = new DownloadResult { exception = result.exception, instanceData = new StreamInstanceData(streamInstance, result.instanceData.syncObject), downloadedDependencies = result.downloadedDependencies }; } else { lock (m_Tasks) { task = DownloadSyncInstanceDependencies(streamInstance, token); m_Tasks[streamInstance.instance.ObjectId] = task; } downloadResult = await task; } m_DownloadedInstances.Enqueue(downloadResult); }
protected override void UpdateInternal(float unscaledDeltaTime) { while (m_DownloadResults.TryDequeue(out var result)) { if (result.exception != null) { m_Hub.Broadcast(new StreamingErrorEvent(result.streamAsset.key, result.streamAsset.boundingBox, result.exception)); continue; } var streamInstance = new StreamInstance(result.streamAsset.key, result.streamInstance, result.streamAsset.boundingBox); var key = result.streamAsset.key; if (m_Cache.TryGetValue(key, out var previousStreamInstance)) { if (previousStreamInstance.instance.ObjectId != result.streamInstance.ObjectId) { m_InstanceDataOutput.SendStreamRemoved(new SyncedData <StreamInstance>(key, previousStreamInstance)); m_InstanceDataOutput.SendStreamAdded(new SyncedData <StreamInstance>(key, streamInstance)); } else { if (m_DirtySyncObject.Contains(key)) { m_DirtySyncObject.Remove(key); m_InstanceDataOutput.SendStreamRemoved(new SyncedData <StreamInstance>(key, previousStreamInstance)); m_InstanceDataOutput.SendStreamAdded(new SyncedData <StreamInstance>(key, streamInstance)); } else { m_InstanceDataOutput.SendStreamChanged(new SyncedData <StreamInstance>(key, streamInstance)); } } } else { m_InstanceDataOutput.SendStreamAdded(new SyncedData <StreamInstance>(key, streamInstance)); } m_Cache[key] = streamInstance; var syncObjectKey = new StreamKey(streamInstance.key.source, PersistentKey.GetKey <SyncObject>(streamInstance.instance.ObjectId)); if (!m_Instances.TryGetValue(syncObjectKey, out var instances)) { m_Instances[syncObjectKey] = instances = new HashSet <StreamAsset>(); } instances.Add(result.streamAsset); } if (m_State == State.WaitingToFinish && m_DownloadRequests.IsEmpty) { m_InstanceDataOutput.SendEnd(); m_State = State.Idle; } }
protected virtual SyncObjectBinding ImportInstance(StreamInstance stream) { var syncObjectBinding = SyncPrefabImporter.CreateInstance(GetInstanceRoot(stream.key.source), stream.key.source, stream.instance, this); #if UNITY_EDITOR var box = stream.boundingBox; var min = new Vector3(box.Min.X, box.Min.Y, box.Min.Z); syncObjectBinding.bounds = new Bounds(min, Vector3.zero); syncObjectBinding.bounds.Encapsulate(new Vector3(box.Max.X, box.Max.Y, box.Max.Z)); #endif return(syncObjectBinding); }
async Task <DownloadResult> DownloadSyncInstanceDependencies(StreamInstance streamInstance, CancellationToken token) { try { var sourceId = streamInstance.key.source; var result = await DownloadSyncModel <SyncObject>(sourceId, streamInstance.instance.ObjectId, token); var syncObject = (SyncObject)result.asset; var tasks = new List <Task <AssetEntry <ISyncModel> > >(); DownloadMeshes(sourceId, syncObject, ref tasks, token); DownloadMaterials(sourceId, syncObject, ref tasks, token); token.ThrowIfCancellationRequested(); await Task.WhenAll(tasks); var downloadResult = new DownloadResult { instanceData = new StreamInstanceData(streamInstance, syncObject), downloadedDependencies = tasks.Select(r => r.Result).ToList() }; return(downloadResult); } catch (Exception ex) { return(new DownloadResult { exception = ex, instanceData = new StreamInstanceData(streamInstance, null) }); } }
public StreamInstanceData(StreamInstance instance, SyncObject syncObject) { this.instance = instance; this.syncObject = syncObject; }