I’ve been working on a Unity Project to automatically detect a new version uploaded to a speckle account, and have it automatically receive the model in the unity gamescene.
The listeners work; when a model is uploaded automatically from Revit, it detects it. However, when it tries to trigger the speckleReceiver.ReceiveAndConvert_Async();, the model does not show up in the gamescene. Below is the code.
using System;
using System.Collections;
using Speckle.ConnectorUnity.Components;
using Speckle.Core.Api;
using Speckle.Core.Models;
using Speckle.Core.Transports;
using Speckle.Core.Credentials;
using UnityEngine;
using Speckle.Core.Kits;
using Speckle.Core.Logging;
using Speckle.Core.Models.GraphTraversal;
//edit below this
public class SpeckleSyncHandler : MonoBehaviour
{
public SpeckleReceiver speckleReceiver;
private Client speckleClient;
private IDisposable subscription;
private void Start()
{
var account = AccountManager.GetDefaultAccount();
if (account == null)
{
Debug.LogError("No Speckle account found!");
return;
}
speckleClient = new Client(account);
// Listen to Project
var streamId = speckleReceiver.Stream.Selected.id;
var subscription = speckleClient.Subscription.CreateProjectVersionsUpdatedSubscription(streamId);
// Attach event handler to react to new versions
subscription.Listeners += OnNewVersionReceived;
Debug.Log("Listening for new versions...");
}
private void OnNewVersionReceived(object sender, EventArgs e)
{
Debug.Log("New version detected.");
if (speckleReceiver != null)
{
speckleReceiver.ReceiveAndConvert_Async();
Debug.Log("Data receive and conversion triggered.");
}
}
private void OnDestroy()
{
subscription?.Dispose();
speckleClient?.Dispose();
}
}
In the console," Listening for new versions…" , “New version detected.”, “Data receive and conversion triggered.” all appear, but the model does not.
I also tested the code below to see whether Speckle Receiver can function during runtime. After pressing the space button and waiting for 1-2 seconds, the model appeared in the gamescene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Speckle.ConnectorUnity.Components;
public class TestReceive : MonoBehaviour
{
private SpeckleReceiver speckleReceiver;
void Start()
{
speckleReceiver = GetComponent<SpeckleReceiver>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (speckleReceiver != null)
{
speckleReceiver.ReceiveAndConvert_Async();
}
}
}
}