Last active
May 11, 2017 20:48
-
-
Save unity3dcollege/e5d699a7c4c6cfff390e3f471be09760 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
public class PoolPreparer : MonoBehaviour | |
{ | |
[SerializeField] | |
GameObject[] prefabs; | |
[SerializeField] | |
private int initialPoolSize = 100; | |
private void Awake() | |
{ | |
foreach(var prefab in prefabs) | |
{ | |
if (prefab == null) | |
{ | |
Debug.LogError("Null prefab in PoolPreparer"); | |
} | |
else | |
{ | |
IPoolable poolablePrefab = prefab.GetComponent<IPoolable>(); | |
if (poolablePrefab == null) | |
{ | |
Debug.LogError("Prefab does not contain an IPoolable and can't be pooled"); | |
} | |
else | |
{ | |
Pool.Prewarm(poolablePrefab, initialPoolSize); | |
} | |
} | |
} | |
} | |
private void OnValidate() | |
{ | |
List<GameObject> prefabsToRemove = new List<GameObject>(); | |
foreach (var prefab in prefabs.Where(t=> t != null)) | |
{ | |
if (PrefabUtility.GetPrefabType(prefab) != PrefabType.Prefab) | |
{ | |
Debug.LogError(string.Format("{0} is not a prefab. It has been removed.", prefab.gameObject.name)); | |
prefabsToRemove.Add(prefab); | |
} | |
IPoolable poolablePrefab = prefab.GetComponent<IPoolable>(); | |
if (poolablePrefab == null) | |
{ | |
Debug.LogError("Prefab does not contain an IPoolable and can't be pooled. It has been removed."); | |
prefabsToRemove.Add(prefab); | |
} | |
} | |
prefabs = prefabs | |
.Where(t => t != null && prefabsToRemove.Contains(t) == false) | |
.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment