| | 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")] |
| | 28 | | public bool usePool; |
| | 29 | |
|
| | 30 | | public int prewarmCount; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public Item[] factoryList; |
| | 34 | |
|
| | 35 | | Dictionary<CLASS_ID_COMPONENT, Item> factoryDict; |
| | 36 | |
|
| 708 | 37 | | public static PoolableComponentFactory Create() { return Resources.Load("RuntimeComponentFactory") as PoolableCo |
| | 38 | |
|
| | 39 | | public void EnsureFactoryDictionary() |
| | 40 | | { |
| 47 | 41 | | if (factoryDict == null) |
| | 42 | | { |
| 1 | 43 | | factoryDict = new Dictionary<CLASS_ID_COMPONENT, Item>(); |
| | 44 | |
|
| 24 | 45 | | for (int i = 0; i < factoryList.Length; i++) |
| | 46 | | { |
| 11 | 47 | | Item item = factoryList[i]; |
| | 48 | |
|
| 11 | 49 | | if (!factoryDict.ContainsKey(item.classId)) |
| | 50 | | { |
| 11 | 51 | | factoryDict.Add(item.classId, item); |
| | 52 | | } |
| | 53 | | } |
| | 54 | | } |
| 47 | 55 | | } |
| | 56 | |
|
| | 57 | | public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component |
| | 58 | | { |
| 2396 | 59 | | for (int i = 0; i < factoryList.Length; i++) |
| | 60 | | { |
| 1129 | 61 | | Item item = factoryList[i]; |
| | 62 | |
|
| 1129 | 63 | | if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null) |
| | 64 | | { |
| 66 | 65 | | return item.classId; |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| 69 | 69 | | return CLASS_ID_COMPONENT.NONE; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public void PrewarmPools() |
| | 73 | | { |
| 35232 | 74 | | for (int i = 0; i < factoryList.Length; i++) |
| | 75 | | { |
| 16148 | 76 | | Item item = factoryList[i]; |
| | 77 | |
|
| 16148 | 78 | | if (item.usePool) |
| | 79 | | { |
| 1468 | 80 | | EnsurePoolForItem(item); |
| 1468 | 81 | | GetPoolForItem(item).ForcePrewarm(); |
| | 82 | | } |
| | 83 | | } |
| 1468 | 84 | | } |
| | 85 | |
|
| 2940 | 86 | | private Pool GetPoolForItem(Item item) { return PoolManager.i.GetPool(GetIdForPool(item)); } |
| | 87 | |
|
| | 88 | | private object GetIdForPool(Item item) |
| | 89 | | { |
| | 90 | | #if UNITY_EDITOR |
| 3624 | 91 | | return item.classId.ToString() + "_POOL"; |
| | 92 | | #else |
| | 93 | | return item.classId; |
| | 94 | | #endif |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private void EnsurePoolForItem(Item item) |
| | 98 | | { |
| 1470 | 99 | | Pool pool = GetPoolForItem(item); |
| | 100 | |
|
| 1470 | 101 | | if (pool != null) |
| 786 | 102 | | return; |
| | 103 | |
|
| 684 | 104 | | GameObject original = Instantiate(item.prefab.gameObject); |
| 684 | 105 | | pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount, isPersistent: |
| 684 | 106 | | pool.useLifecycleHandlers = true; |
| 684 | 107 | | } |
| | 108 | |
|
| | 109 | | public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id) |
| | 110 | | where ItemType : IPoolableObjectContainer |
| | 111 | | { |
| 47 | 112 | | EnsureFactoryDictionary(); |
| | 113 | |
|
| 47 | 114 | | if (!factoryDict.ContainsKey(id)) |
| | 115 | | { |
| | 116 | | #if UNITY_EDITOR |
| 0 | 117 | | Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!"); |
| | 118 | | #endif |
| 0 | 119 | | return default(ItemType); |
| | 120 | | } |
| | 121 | |
|
| 47 | 122 | | var factoryItem = factoryDict[id]; |
| | 123 | |
|
| 47 | 124 | | if (factoryItem.prefab == null) |
| | 125 | | { |
| 0 | 126 | | Debug.LogError("Prefab for class " + id + " is null!"); |
| 0 | 127 | | return default(ItemType); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | GameObject instancedGo; |
| 47 | 131 | | PoolableObject poolableObject = null; |
| | 132 | |
|
| 47 | 133 | | if (factoryItem.usePool) |
| | 134 | | { |
| 2 | 135 | | EnsurePoolForItem(factoryItem); |
| 2 | 136 | | poolableObject = GetPoolForItem(factoryItem).Get(); |
| 2 | 137 | | instancedGo = poolableObject.gameObject; |
| 2 | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 45 | 141 | | instancedGo = Instantiate(factoryItem.prefab.gameObject); |
| | 142 | | } |
| | 143 | |
|
| 47 | 144 | | ItemType item = instancedGo.GetComponent<ItemType>(); |
| 47 | 145 | | item.poolableObject = poolableObject; |
| | 146 | |
|
| 47 | 147 | | return item; |
| | 148 | | } |
| | 149 | | } |
| | 150 | | } |