chenxin
2020-11-06 fe4457c2ff07dcea1cccd6886188a334ef9c196b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEngine;
 
namespace Core.Utilities
{
    /// <summary>
    /// Class that is to be pooled
    /// </summary>
    public class Poolable : MonoBehaviour
    {
        /// <summary>
        /// Number of poolables the pool will initialize
        /// </summary>
        public int initialPoolCapacity = 10;
 
        /// <summary>
        /// Pool that this poolable belongs to
        /// </summary>
        public Pool<Poolable> pool;
 
        /// <summary>
        /// Repool this instance, and move us under the poolmanager
        /// </summary>
        protected virtual void Repool()
        {
            transform.SetParent(PoolManager.instance.transform, false);
            pool.Return(this);
        }
 
        /// <summary>gameObject
        /// Pool the object if possible, otherwise destroy it
        /// </summary>
        /// <param name="gameObject">GameObject attempting to pool</param>
        public static void TryPool(GameObject gameObject)
        {
            var poolable = gameObject.GetComponent<Poolable>();
            if (poolable != null && poolable.pool != null && PoolManager.instanceExists)
            {
                poolable.Repool();
            }
            else
            {
                Destroy(gameObject);
            }
        }
 
        /// <summary>
        /// If the prefab is poolable returns a pooled object otherwise instantiates a new object
        /// </summary>
        /// <param name="prefab">Prefab of object required</param>
        /// <typeparam name="T">Component type</typeparam>
        /// <returns>The pooled or instantiated component</returns>
        public static T TryGetPoolable<T>(GameObject prefab) where T : Component
        {
            var poolable = prefab.GetComponent<Poolable>();
            T instance = poolable != null && PoolManager.instanceExists ? 
                PoolManager.instance.GetPoolable(poolable).GetComponent<T>() : Instantiate(prefab).GetComponent<T>();
            return instance;
        }
 
        /// <summary>
        /// If the prefab is poolable returns a pooled object otherwise instantiates a new object
        /// </summary>
        /// <param name="prefab">Prefab of object required</param>
        /// <returns>The pooled or instantiated gameObject</returns>
        public static GameObject TryGetPoolable(GameObject prefab)
        {
            var poolable = prefab.GetComponent<Poolable>();
            GameObject instance = poolable != null && PoolManager.instanceExists ? 
                PoolManager.instance.GetPoolable(poolable).gameObject : Instantiate(prefab);
            return instance;
        }
    }
}