|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Linq; |
| 3 | +using UnityEngine; |
3 | 4 |
|
4 | 5 | namespace TNRD |
5 | 6 | { |
@@ -58,5 +59,61 @@ public static SerializableInterface<T>[] ToSerializableInterfaceArray<T>(this IE |
58 | 59 | { |
59 | 60 | return list.Select(e => new SerializableInterface<T>(e)).ToArray(); |
60 | 61 | } |
| 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 | + } |
61 | 118 | } |
62 | 119 | } |
0 commit comments