PrePooler.cs 941 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for pre-creating the pools before they are needed.
  5. /// This is useful for objects like bullets, which are not part of a PooledLootTable
  6. /// (which would automatically pool the objects).
  7. /// </summary>
  8. public class PrePooler : MonoBehaviour
  9. {
  10. [System.Serializable]
  11. public class PrePoolInfo
  12. {
  13. public PoolObject prefab;
  14. public int quantity;
  15. }
  16. public PrePoolInfo[] prePools;
  17. void Awake()
  18. {
  19. PrePool();
  20. NotificationCenter.AddListener(OnApplySkin, NotificationType.ApplySkin);
  21. }
  22. void OnDestroy()
  23. {
  24. NotificationCenter.RemoveListener(OnApplySkin, NotificationType.ApplySkin);
  25. }
  26. void OnApplySkin(Notification note)
  27. {
  28. DestroyImmediate(GameObject.Find("Pools"));
  29. Pool.ClearAllPools();
  30. PrePool();
  31. }
  32. void PrePool()
  33. {
  34. foreach (PrePoolInfo prePool in prePools)
  35. {
  36. Pool.Create(prePool.prefab, prePool.quantity);
  37. }
  38. }
  39. }