From 5879f407a84969e9df80060e9b216594034fd913 Mon Sep 17 00:00:00 2001 From: marcellsimo Date: Thu, 8 Feb 2024 11:12:20 +0100 Subject: [PATCH 1/3] Support for instantiating --- Runtime/Extensions.cs | 72 ++++++++++++++++++++++++++++++++ Runtime/SerializableInterface.cs | 9 ++++ 2 files changed, 81 insertions(+) diff --git a/Runtime/Extensions.cs b/Runtime/Extensions.cs index f832c89..33fbf2f 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,76 @@ 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); + + if(instantiatedObject is GameObject) + { + GameObject go = (GameObject)instantiatedObject; + return go.GetComponent(); + } + else + { + return instantiatedObject as TInterface; + } + } + + 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 go = (GameObject)instantiatedObject; + if (!go.TryGetComponent(out TInterface tInterface)) return null; + return tInterface; + } + else + { + if (instantiatedObject is TInterface is false) return 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; + } } } From 98ad5b614051bdc5ab4ba72307cc1b67c2d8b4f0 Mon Sep 17 00:00:00 2001 From: marcellsimo Date: Thu, 8 Feb 2024 11:34:51 +0100 Subject: [PATCH 2/3] Refactoring --- Runtime/Extensions.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Runtime/Extensions.cs b/Runtime/Extensions.cs index 33fbf2f..07f57ea 100644 --- a/Runtime/Extensions.cs +++ b/Runtime/Extensions.cs @@ -68,16 +68,8 @@ public static TInterface Instantiate(this SerializableInterface(); - } - else - { - return instantiatedObject as TInterface; - } + + return GetInterfaceReference(instantiatedObject); } public static TInterface Instantiate(this SerializableInterface serializableInterface, Transform parent) where TInterface : class From a54dd5000c3b23f93984422752e95156158646b2 Mon Sep 17 00:00:00 2001 From: marcellsimo <83577850+sima995@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:33:47 +0100 Subject: [PATCH 3/3] readability improvement Co-authored-by: Christiaan Bloemendaal --- Runtime/Extensions.cs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Runtime/Extensions.cs b/Runtime/Extensions.cs index 07f57ea..035be7b 100644 --- a/Runtime/Extensions.cs +++ b/Runtime/Extensions.cs @@ -110,17 +110,10 @@ public static TInterface Instantiate(this SerializableInterface(Object instantiatedObject) where TInterface : class { - if (instantiatedObject is GameObject) - { - GameObject go = (GameObject)instantiatedObject; - if (!go.TryGetComponent(out TInterface tInterface)) return null; - return tInterface; - } - else - { - if (instantiatedObject is TInterface is false) return null; - return instantiatedObject as TInterface; - } + if (instantiatedObject is GameObject gameObject) + return gameObject.TryGetComponent(out TInterface component) ? component : null; + + return instantiatedObject as TInterface; } } }