Skip to content

Commit de0b647

Browse files
Thundernerdsima995
andauthored
feat: instantiation support (#62)
Co-authored-by: marcellsimo <83577850+sima995@users.noreply.github.com>
1 parent 2a7a02f commit de0b647

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Runtime/Extensions.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using UnityEngine;
34

45
namespace TNRD
56
{
@@ -58,5 +59,61 @@ public static SerializableInterface<T>[] ToSerializableInterfaceArray<T>(this IE
5859
{
5960
return list.Select(e => new SerializableInterface<T>(e)).ToArray();
6061
}
62+
63+
public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> serializableInterface) where TInterface : class
64+
{
65+
if (!serializableInterface.TryGetObject(out Object unityObject))
66+
{
67+
throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object");
68+
}
69+
70+
Object instantiatedObject = Object.Instantiate(unityObject);
71+
72+
return GetInterfaceReference<TInterface>(instantiatedObject);
73+
}
74+
75+
public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> serializableInterface, Transform parent) where TInterface : class
76+
{
77+
if (!serializableInterface.TryGetObject(out Object unityObject))
78+
{
79+
throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object");
80+
}
81+
82+
Object instantiatedObject = Object.Instantiate(unityObject, parent);
83+
84+
return GetInterfaceReference<TInterface>(instantiatedObject);
85+
}
86+
87+
public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> serializableInterface, Vector3 position, Quaternion rotation) where TInterface : class
88+
{
89+
if (!serializableInterface.TryGetObject(out Object unityObject))
90+
{
91+
throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object");
92+
}
93+
94+
Object instantiatedObject = Object.Instantiate(unityObject, position, rotation);
95+
96+
return GetInterfaceReference<TInterface>(instantiatedObject);
97+
}
98+
99+
public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> serializableInterface, Vector3 position, Quaternion rotation, Transform parent) where TInterface : class
100+
{
101+
if (!serializableInterface.TryGetObject(out Object unityObject))
102+
{
103+
throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object");
104+
}
105+
106+
Object instantiatedObject = Object.Instantiate(unityObject, position, rotation, parent);
107+
108+
return GetInterfaceReference<TInterface>(instantiatedObject);
109+
}
110+
111+
private static TInterface GetInterfaceReference<TInterface>(Object instantiatedObject) where TInterface : class
112+
{
113+
if (instantiatedObject is GameObject gameObject)
114+
return gameObject.TryGetComponent(out TInterface component) ? component : null;
115+
116+
return instantiatedObject as TInterface;
117+
}
61118
}
62119
}

Runtime/SerializableInterface.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,14 @@ object ISerializableInterface.GetRawReference()
5757
{
5858
return rawReference;
5959
}
60+
61+
public bool TryGetObject(out UnityEngine.Object unityObject)
62+
{
63+
unityObject = null;
64+
if (mode != ReferenceMode.Unity) return false;
65+
66+
unityObject = unityReference;
67+
return true;
68+
}
6069
}
6170
}

0 commit comments

Comments
 (0)