public static Object Instantiate(Object original, Transform parent = null, bool instantiateInWorldSpace = false) { if (!original) { Debug.LogError("original cannot be null!"); return(null); } if (original is Component || original is GameObject) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { Object obj = original is Component? poolBhv.Instantiate(original.GetType(), parent, instantiateInWorldSpace) as Object : poolBhv.Instantiate(parent, instantiateInWorldSpace) as Object ; return(obj); } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, parent, instantiateInWorldSpace)); #else return(Object.Instantiate(original)); #endif } } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, parent, instantiateInWorldSpace)); #else return(Object.Instantiate(original)); #endif } }
/// <summary> /// Instantiates a new instance using the prefab pool or creating a new pool if needed. /// </summary> /// <param name="original"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="parent"></param> /// <returns></returns> public static Object Instantiate(Object original, Vector3 position, Quaternion rotation = default(Quaternion), Transform parent = null) { if (!original) { Debug.LogError("original cannot be null!"); return(null); } if (original is Component || original is GameObject) { GameObject go = GetObjectAsGameObject(original); PoolBehaviour poolBhv = FindOrCreatePool(go); if (poolBhv) { Object obj = original is Component? poolBhv.Instantiate(original.GetType(), position, rotation, parent) as Object : poolBhv.Instantiate(position, rotation, parent) as Object ; return(obj); } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, position, rotation, parent)); #else return(Object.Instantiate(original, position, rotation)); #endif } } else { #if UNITY_2017_1_OR_NEWER return(Object.Instantiate(original, position, rotation, parent)); #else return(Object.Instantiate(original, position, rotation)); #endif } }