public GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) { if (prefab != null) { var gameObjectPool = default(LeanGameObjectPool); if (LeanGameObjectPool.TryFindPoolByPrefab(prefab, ref gameObjectPool) == false) { gameObjectPool = new GameObject(prefab.name + " Pool").AddComponent <LeanGameObjectPool>(); gameObjectPool.transform.SetParent(_pool.transform); gameObjectPool.Prefab = prefab; } var clone = gameObjectPool.Spawn(position, rotation, parent); if (clone != null) { if (gameObjectPool.Recycle && gameObjectPool.Spawned >= gameObjectPool.Capacity) { var existingPool = default(LeanGameObjectPool); if (Links.TryGetValue(clone, out existingPool)) { if (existingPool != gameObjectPool) { Links.Remove(clone); } else { return(clone.gameObject); } } } Links.Add(clone, gameObjectPool); return(clone.gameObject); } } else { Debug.LogError("Attempting to spawn a null prefab"); } return(null); }
/// <summary> /// 池中预加载一定数量目标预制体的实例 /// </summary> /// <param name="prefab">目标预制体</param> /// <param name="size">预加载数量</param> public void Preload(GameObject prefab, int size = 5) { if (prefab != null) { var gameObjectPool = default(LeanGameObjectPool); if (LeanGameObjectPool.TryFindPoolByPrefab(prefab, ref gameObjectPool) == false) { gameObjectPool = new GameObject(prefab.name + " Pool").AddComponent <LeanGameObjectPool>(); gameObjectPool.transform.SetParent(_pool.transform); gameObjectPool.Prefab = prefab; } gameObjectPool.Preload = size; gameObjectPool.PreloadAll(); } else { Debug.LogError("无法为 null 预制体做预加载"); } }