wip update for the sample project to use new ui components

This commit is contained in:
Jedd Morgan
2023-06-26 22:13:12 +01:00
parent 62875c0a27
commit f406cb8229
14 changed files with 2912 additions and 18 deletions
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c62e8ef712f821d418d1a8a248e75df3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,61 @@
#nullable enable
using Speckle.Core.Kits;
using Speckle.Core.Logging;
using Speckle.Core.Models;
namespace Speckle.ConnectorUnity.Utils
{
public static class CoreUtils
{
public static void SetupInit()
{
Setup.Init(HostApplications.Unity.GetVersion(GetHostAppVersion()), HostApplications.Unity.Slug);
}
public static HostAppVersion GetHostAppVersion()
{
#if UNITY_2019
return HostAppVersion.v2019;
#elif UNITY_2020
return HostAppVersion.v2020;
#elif UNITY_2021
return HostAppVersion.v2021;
#elif UNITY_2022
return HostAppVersion.v2022;
#elif UNITY_2023
return HostAppVersion.v2023;
#elif UNITY_2024
return HostAppVersion.v2024;
#elif UNITY_2025
return HostAppVersion.v2025;
#else
return HostAppVersion.v;
#endif
}
public const string ObjectNameSeparator = " -- ";
/// <param name="speckleObject">The object to be named</param>
/// <returns>A human-readable Object name unique to the given <paramref name="speckleObject"/></returns>
public static string GenerateObjectName(Base speckleObject)
{
var prefix = GetFriendlyObjectName(speckleObject) ?? SimplifiedSpeckleType(speckleObject);
return $"{prefix}{ObjectNameSeparator}{speckleObject.id}";
}
public static string? GetFriendlyObjectName(Base speckleObject)
{
return speckleObject["name"] as string
?? speckleObject["Name"] as string
?? speckleObject["family"] as string;
}
/// <param name="speckleObject"></param>
/// <returns>The most significant type in a given <see cref="Base.speckle_type"/></returns>
public static string SimplifiedSpeckleType(Base speckleObject)
{
return speckleObject.speckle_type.Split(':')[^1];
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 280d43bfe5dc1d14181295ae5c365183
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,28 @@
#nullable enable
using System.Linq;
using Speckle.Core.Models;
namespace Speckle.ConnectorUnity.SpeckleUtils
{
/// <summary>
/// Extension methods for <see cref="Base"/> object models
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// Sets a property dynamically, checking if an instance prop with the same name exists
/// </summary>
/// <param name="speckleObject"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
#pragma warning disable CS0618
public static void SetDetachedPropertyChecked(this Base speckleObject, string propertyName, object? value)
{
if(speckleObject.GetInstanceMembersNames().Any(name => name == propertyName))
speckleObject[propertyName] = value;
else
speckleObject[$"@{propertyName}"] = value;
}
#pragma warning restore CS0618
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f88837bf55bd20642a1aed6b2a3b6b81
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,14 @@
{
"name": "Speckle.ConnectorUnity.SpeckleUtils",
"rootNamespace": "Speckle.ConnectorUnity",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": true
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e71520b51b4847d45ab0b1dacbf935c5
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d74d43435d909534d9b12f5ed3758603
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,155 @@
#nullable enable
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
namespace Speckle.ConnectorUnity.UnityUtils
{
public static class EngineUtils
{
public static void SafeDestroy(UnityEngine.Object obj)
{
if (Application.isPlaying)
UnityEngine.Object.Destroy(obj);
else
UnityEngine.Object.DestroyImmediate(obj);
}
public static bool Valid(this string name) => !string.IsNullOrEmpty(name);
public static Mesh SafeMeshGet(this MeshFilter mf) => Application.isPlaying ? mf.mesh : mf.sharedMesh;
public static void SafeMeshSet(this GameObject go, Mesh m, Material[] materials, bool addMeshComponentsIfNotFound = true)
{
MeshFilter? mf = go.GetComponent<MeshFilter>();
MeshRenderer? mr = go.GetComponent<MeshRenderer>();
if (addMeshComponentsIfNotFound)
{
if (mf == null)
mf = go.AddComponent<MeshFilter>();
if (mr == null)
mr = go.AddComponent<MeshRenderer>();
}
if (Application.isPlaying)
{
mf.mesh = m;
mr.materials = materials;
}
else
{
mf.sharedMesh = m;
mr.sharedMaterials = materials;
}
}
/// <summary>
/// Converts a Unity color to an ARBG int
/// </summary>
public static int ToIntColor(this Color c)
{
return
System.Drawing.Color
.FromArgb(Convert.ToInt32(c.r * 255), Convert.ToInt32(c.g * 255), Convert.ToInt32(c.b * 255))
.ToArgb();
}
/// <summary>
/// Converts a ARGB formatted int to a Unity Color
/// </summary>
public static Color ToUnityColor(this int c)
{
var argb = System.Drawing.Color.FromArgb(c);
return new Color(argb.R / 255f, argb.G / 255f, argb.B / 255f);
}
public static IEnumerator GetImageRoutine(string url, string authToken, Action<Texture2D?> callback)
{
using UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
www.SetRequestHeader("Authorization", $"Bearer {authToken}");
UnityWebRequestAsyncOperation request = www.SendWebRequest();
yield return request;
if(www.result != UnityWebRequest.Result.Success )
{
bool isDataError = www.result == UnityWebRequest.Result.DataProcessingError;
string error = isDataError
? $"{www.result}: {www.downloadHandler.error}"
: www.error;
Debug.LogWarning( $"Error fetching image from {www.url}: {error}" );
yield break;
}
Texture2D? texture = DownloadHandlerTexture.GetContent(www);
callback.Invoke(texture);
}
/// <summary>
/// Coroutine <see cref="CustomYieldInstruction"/> that starts and waits for an async <see cref="System.Threading.Tasks.Task"/>
/// to complete.
/// </summary>
/// <remarks>Useful for running async tasks from coroutines</remarks>
public class WaitForTask : CustomYieldInstruction
{
public readonly Task Task;
public override bool keepWaiting => !Task.IsCompleted;
public WaitForTask(Func<Task> function)
{
Task = Task.Run(function);
}
}
/// <inheritdoc cref="WaitForTask"/>
public sealed class WaitForTask<TResult> : CustomYieldInstruction
{
public readonly Task<TResult> Task;
public TResult Result => Task.Result;
public override bool keepWaiting => !Task.IsCompleted;
public WaitForTask(Func<Task<TResult>> function)
{
this.Task = System.Threading.Tasks.Task.Run(function);
}
}
public sealed class PerformanceThrottle : IEnumerator
{
private readonly IEnumerator _routine;
public PerformanceThrottle(IEnumerator routine)
{
_routine = routine;
}
public bool MoveNext()
{
while (_routine.MoveNext())
{
_ = 0;
}
return false;
}
public void Reset()
{
_routine.Reset();
}
public object? Current => _routine.Current;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 98317fb9e5c090c46a7802a177a3c691
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,14 @@
{
"name": "Speckle.ConnectorUnity.Utils.Unity",
"rootNamespace": "Speckle.ConnectorUnity.Utils.Unity",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 13c1dc1fefb5ca44aa3d755118da5f6c
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -122,5 +122,6 @@ namespace Speckle.ConnectorUnity.Utils
this.Task = System.Threading.Tasks.Task.Run(function);
}
}
}
}