/
AttachSpecklePropertiesExample.cs
62 lines (54 loc) · 1.94 KB
/
AttachSpecklePropertiesExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;
using Objects.Converter.Unity;
using Speckle.ConnectorUnity.Utils;
using Speckle.ConnectorUnity.Wrappers;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Models;
using Speckle.Core.Transports;
using UnityEngine;
/// <summary>
/// Example script for grabbing speckle properties for a specific object "on-the-fly"
/// </summary>
/// <remarks>
/// see discussion https://speckle.community/t/reloading-assemblies-takes-too-long/6708
/// </remarks>
[AddComponentMenu("Speckle/Extras/" + nameof(AttachSpecklePropertiesExample))]
public class AttachSpecklePropertiesExample : MonoBehaviour
{
public string streamId;
public string objectId;
public virtual void Start()
{
Client speckleClient = new(AccountManager.GetDefaultAccount());
StartCoroutine(AttachSpeckleProperties(speckleClient, streamId, objectId));
}
public IEnumerator AttachSpeckleProperties(
Client speckleClient,
string streamId,
string objectId
)
{
//Fetch the object from Speckle
ServerTransport remoteTransport = new(speckleClient.Account, streamId);
Utils.WaitForTask<Base> operation =
new(async () => await Operations.Receive(objectId, remoteTransport));
//yield until task completes
yield return operation;
Base speckleObject = operation.Result;
//Do something with the properties. e.g. attach SpeckleProperties component
DoSomething(speckleObject);
}
protected virtual void DoSomething(Base speckleObject)
{
//GetProperties will filter "useful" properties
Dictionary<string, object> properties = ConverterUnity.GetProperties(
speckleObject,
typeof(SpeckleObject)
);
var sd = this.gameObject.AddComponent<SpeckleProperties>();
sd.Data = properties;
sd.SpeckleType = speckleObject.GetType();
}
}