Skip to content

Commit

Permalink
Merge pull request #61 from sima995/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Feb 9, 2024
2 parents 2a7a02f + a54dd50 commit fd0f799
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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 class Extensions
{
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;
}
}
}

0 comments on commit fd0f799

Please sign in to comment.