diff --git a/Runtime/Extensions.cs b/Runtime/Extensions.cs index f832c89..035be7b 100644 --- a/Runtime/Extensions.cs +++ b/Runtime/Extensions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using UnityEngine; namespace TNRD { @@ -58,5 +59,61 @@ public static SerializableInterface[] ToSerializableInterfaceArray(this IE { return list.Select(e => new SerializableInterface(e)).ToArray(); } + + public static TInterface Instantiate(this SerializableInterface serializableInterface) where TInterface : class + { + if (!serializableInterface.TryGetObject(out Object unityObject)) + { + throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object"); + } + + Object instantiatedObject = Object.Instantiate(unityObject); + + return GetInterfaceReference(instantiatedObject); + } + + public static TInterface Instantiate(this SerializableInterface serializableInterface, Transform parent) where TInterface : class + { + if (!serializableInterface.TryGetObject(out Object unityObject)) + { + throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object"); + } + + Object instantiatedObject = Object.Instantiate(unityObject, parent); + + return GetInterfaceReference(instantiatedObject); + } + + public static TInterface Instantiate(this SerializableInterface serializableInterface, Vector3 position, Quaternion rotation) where TInterface : class + { + if (!serializableInterface.TryGetObject(out Object unityObject)) + { + throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object"); + } + + Object instantiatedObject = Object.Instantiate(unityObject, position, rotation); + + return GetInterfaceReference(instantiatedObject); + } + + public static TInterface Instantiate(this SerializableInterface serializableInterface, Vector3 position, Quaternion rotation, Transform parent) where TInterface : class + { + if (!serializableInterface.TryGetObject(out Object unityObject)) + { + throw new System.Exception($"Cannot instantiate {serializableInterface} because it's has no reference of type UnityEngine.Object"); + } + + Object instantiatedObject = Object.Instantiate(unityObject, position, rotation, parent); + + return GetInterfaceReference(instantiatedObject); + } + + private static TInterface GetInterfaceReference(Object instantiatedObject) where TInterface : class + { + if (instantiatedObject is GameObject gameObject) + return gameObject.TryGetComponent(out TInterface component) ? component : null; + + return instantiatedObject as TInterface; + } } } diff --git a/Runtime/SerializableInterface.cs b/Runtime/SerializableInterface.cs index 2376d77..07c5729 100644 --- a/Runtime/SerializableInterface.cs +++ b/Runtime/SerializableInterface.cs @@ -57,5 +57,14 @@ object ISerializableInterface.GetRawReference() { return rawReference; } + + public bool TryGetObject(out UnityEngine.Object unityObject) + { + unityObject = null; + if (mode != ReferenceMode.Unity) return false; + + unityObject = unityReference; + return true; + } } }