Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Runtime/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace TNRD
{
Expand Down Expand Up @@ -58,5 +59,61 @@ public static SerializableInterface<T>[] ToSerializableInterfaceArray<T>(this IE
{
return list.Select(e => new SerializableInterface<T>(e)).ToArray();
}

public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> 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<TInterface>(instantiatedObject);
}

public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> 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<TInterface>(instantiatedObject);
}

public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> 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<TInterface>(instantiatedObject);
}

public static TInterface Instantiate<TInterface>(this SerializableInterface<TInterface> 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<TInterface>(instantiatedObject);
}

private static TInterface GetInterfaceReference<TInterface>(Object instantiatedObject) where TInterface : class
{
if (instantiatedObject is GameObject gameObject)
return gameObject.TryGetComponent(out TInterface component) ? component : null;

return instantiatedObject as TInterface;
}
}
}
9 changes: 9 additions & 0 deletions Runtime/SerializableInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}