-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityMethods.cs
More file actions
30 lines (26 loc) · 1.29 KB
/
Copy pathUnityMethods.cs
File metadata and controls
30 lines (26 loc) · 1.29 KB
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
namespace UnityIceFebruary
{
using IceFebruary;
using System;
using System.Runtime.CompilerServices;
using UnityObject = UnityEngine.Object;
/// <summary>
/// Static class that converts Unity components into bridge components and stores them in a list.
/// </summary>
public static class UnityMethods
{
private static readonly ConditionalWeakTable<UnityObject, IBaseEntity> _objects = new();
/// <summary>
/// Returns an already converted Unity component from the list, or converts a Unity component to its bridge counterpart, adds it to the list, and returns it.
/// </summary>
public static TConversion Upsert<T, TConversion>(T unityObject) where T : UnityObject where TConversion : IBaseEntity => unityObject != null && UnityMatchObject.FabricAliases.TryGetValue(unityObject.GetType(), out Func<UnityObject, IBaseEntity> factory) ? (TConversion)_objects.GetValue(unityObject, obj => factory((T)obj)) : default;
/// <summary>
/// Removes the converted Unity component from the list, if it is there.
/// </summary>
public static void Remove<T>(UnityBaseEntity<T> analog) where T : UnityObject
{
if (analog != null)
_objects.Remove(analog.Original);
}
}
}