| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public interface IPoolableComponentFactory |
| | 10 | | { |
| | 11 | | void EnsureFactoryDictionary(); |
| | 12 | | CLASS_ID_COMPONENT GetIdForType<T>() where T : Component; |
| | 13 | | void PrewarmPools(); |
| | 14 | |
|
| | 15 | | ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id) |
| | 16 | | where ItemType : IPoolableObjectContainer; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class PoolableComponentFactory : ScriptableObject, IPoolableComponentFactory |
| | 20 | | { |
| | 21 | | [System.Serializable] |
| | 22 | | public class Item |
| | 23 | | { |
| | 24 | | public CLASS_ID_COMPONENT classId; |
| | 25 | | public Component prefab; |
| | 26 | |
|
| | 27 | | [Header("Pool Options")] public bool usePool; |
| | 28 | |
|
| | 29 | | public int prewarmCount; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public Item[] factoryList; |
| | 33 | |
|
| | 34 | | Dictionary<CLASS_ID_COMPONENT, Item> factoryDict; |
| | 35 | |
|
| | 36 | | public static PoolableComponentFactory Create() |
| | 37 | | { |
| 0 | 38 | | return Resources.Load("RuntimeComponentFactory") as PoolableComponentFactory; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public void EnsureFactoryDictionary() |
| | 42 | | { |
| 65 | 43 | | if (factoryDict == null) |
| | 44 | | { |
| 2 | 45 | | factoryDict = new Dictionary<CLASS_ID_COMPONENT, Item>(); |
| | 46 | |
|
| 48 | 47 | | for (int i = 0; i < factoryList.Length; i++) |
| | 48 | | { |
| 22 | 49 | | Item item = factoryList[i]; |
| | 50 | |
|
| 22 | 51 | | if (!factoryDict.ContainsKey(item.classId)) |
| | 52 | | { |
| 22 | 53 | | factoryDict.Add(item.classId, item); |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| 65 | 57 | | } |
| | 58 | |
|
| | 59 | | public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component |
| | 60 | | { |
| 2552 | 61 | | for (int i = 0; i < factoryList.Length; i++) |
| | 62 | | { |
| 1206 | 63 | | Item item = factoryList[i]; |
| | 64 | |
|
| 1206 | 65 | | if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null) |
| | 66 | | { |
| 76 | 67 | | return item.classId; |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 70 | 71 | | return CLASS_ID_COMPONENT.NONE; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public void PrewarmPools() |
| | 75 | | { |
| 9528 | 76 | | for (int i = 0; i < factoryList.Length; i++) |
| | 77 | | { |
| 4367 | 78 | | Item item = factoryList[i]; |
| | 79 | |
|
| 4367 | 80 | | if (item.usePool) |
| | 81 | | { |
| 397 | 82 | | EnsurePoolForItem(item); |
| 397 | 83 | | GetPoolForItem(item).ForcePrewarm(); |
| | 84 | | } |
| | 85 | | } |
| 397 | 86 | | } |
| | 87 | |
|
| | 88 | | private Pool GetPoolForItem(Item item) |
| | 89 | | { |
| 804 | 90 | | return PoolManager.i.GetPool(GetIdForPool(item)); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private object GetIdForPool(Item item) |
| | 94 | | { |
| | 95 | | #if UNITY_EDITOR |
| 1200 | 96 | | return item.classId.ToString() + "_POOL"; |
| | 97 | | #else |
| | 98 | | return item.classId; |
| | 99 | | #endif |
| | 100 | | } |
| | 101 | |
|
| | 102 | | private void EnsurePoolForItem(Item item) |
| | 103 | | { |
| 402 | 104 | | Pool pool = GetPoolForItem(item); |
| | 105 | |
|
| 402 | 106 | | if (pool != null) |
| 6 | 107 | | return; |
| | 108 | |
|
| 396 | 109 | | GameObject original = Instantiate(item.prefab.gameObject); |
| 396 | 110 | | pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount, |
| | 111 | | isPersistent: true); |
| 396 | 112 | | pool.useLifecycleHandlers = true; |
| 396 | 113 | | } |
| | 114 | |
|
| | 115 | | public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id) |
| | 116 | | where ItemType : IPoolableObjectContainer |
| | 117 | | { |
| 65 | 118 | | EnsureFactoryDictionary(); |
| | 119 | |
|
| 65 | 120 | | if (!factoryDict.ContainsKey(id)) |
| | 121 | | { |
| | 122 | | #if UNITY_EDITOR |
| 0 | 123 | | Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!"); |
| | 124 | | #endif |
| 0 | 125 | | return default(ItemType); |
| | 126 | | } |
| | 127 | |
|
| 65 | 128 | | var factoryItem = factoryDict[id]; |
| | 129 | |
|
| 65 | 130 | | if (factoryItem.prefab == null) |
| | 131 | | { |
| 0 | 132 | | Debug.LogError("Prefab for class " + id + " is null!"); |
| 0 | 133 | | return default(ItemType); |
| | 134 | | } |
| | 135 | |
|
| | 136 | | GameObject instancedGo; |
| 65 | 137 | | PoolableObject poolableObject = null; |
| | 138 | |
|
| 65 | 139 | | if (factoryItem.usePool) |
| | 140 | | { |
| 5 | 141 | | EnsurePoolForItem(factoryItem); |
| 5 | 142 | | poolableObject = GetPoolForItem(factoryItem).Get(); |
| 5 | 143 | | instancedGo = poolableObject.gameObject; |
| 5 | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 60 | 147 | | instancedGo = Instantiate(factoryItem.prefab.gameObject); |
| | 148 | | } |
| | 149 | |
|
| 65 | 150 | | ItemType item = instancedGo.GetComponent<ItemType>(); |
| 65 | 151 | | item.poolableObject = poolableObject; |
| | 152 | |
|
| 65 | 153 | | return item; |
| | 154 | | } |
| | 155 | | } |
| | 156 | | } |