Exemplo n.º 1
0
        /// <summary>
        /// Initializes all the pools set up by user in the editor, and adds them to the Dictionary.
        /// </summary>
        void Awake()
        {
            instance = this;

            parentTransform = this.transform;

            Pools.Clear();
            itemsMarkedForDeletion.Clear();

            if (!usePoolManager)
            {
                return;
            }

            // loop through all the pre-allocated pools and initialize all the pool
            for (var i = 0; i < prefabPoolOptions.Count; ++i)
            {
                var item            = prefabPoolOptions[i];
                var prefabTransform = item.prefabTransform;
                var name            = prefabTransform.name;

                if (item.instancesToPreload <= 0 && !item.poolCanGrow)
                {
                    itemsMarkedForDeletion.Add(item);
                    continue; // no need to pre-allocate any game obj, nothing else to do
                }

                if (prefabTransform == null)
                {
                    Debug.LogWarning("Item at index " + (i + 1) + " in the Pool has no prefab !");
                    continue;
                }

                if (Pools.ContainsKey(name))
                {
                    Debug.LogWarning("Duplicates found in the Pool : " + name);
                }

                // pre-allocate the game objs
                var tmpList = new List <Transform>();

                for (var j = 0; j < item.instancesToPreload; ++j)
                {
                    var newTransform = GameObject.Instantiate(prefabTransform, Vector3.zero, prefabTransform.rotation) as Transform;
                    newTransform.name   = name;
                    newTransform.parent = parentTransform;
                    newTransform.gameObject.SetActive(false);

                    tmpList.Add(newTransform);
                }

                var newPrefabPool = new EZ_PrefabPool(tmpList);
                newPrefabPool.showDebugLog    = item.showDebugLog;
                newPrefabPool.poolCanGrow     = item.poolCanGrow;
                newPrefabPool.parentTransform = parentTransform;

                newPrefabPool.cullDespawned = item.cullDespawned;
                newPrefabPool.cullAbove     = item.cullAbove;
                newPrefabPool.cullDelay     = item.cullDelay;
                newPrefabPool.cullAmount    = item.cullAmount;

                newPrefabPool.enableHardLimit = item.enableHardLimit;
                newPrefabPool.hardLimit       = item.hardLimit;

                newPrefabPool.recycle = item.recycle;

                Pools.Add(name, newPrefabPool); //add the pool to the Dictionary
            }

            foreach (var item in itemsMarkedForDeletion)
            {
                prefabPoolOptions.Remove(item);
            }

            itemsMarkedForDeletion.Clear();
        }
Exemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            EditorGUI.indentLevel = 0;
            GUI.contentColor      = Color.white;
            bool isDirty = false;

            poolManager = (EZ_PoolManager)target;

            EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolManagerItemLogo);

            poolManager.autoAddMissingPrefabPool = EditorGUILayout.Toggle("Auto Add Missing Items", poolManager.autoAddMissingPrefabPool);
            poolManager.showDebugLog             = EditorGUILayout.Toggle("Show Debug Log", poolManager.showDebugLog);
            poolManager.usePoolManager           = EditorGUILayout.Toggle("Use EZ Pool Manager", poolManager.usePoolManager);

            EditorGUILayout.Separator();
            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);

            EditorGUI.indentLevel = 1;

            poolManager.isRootExpanded = EditorGUILayout.Foldout(poolManager.isRootExpanded, string.Format("Pools ({0})", poolManager.prefabPoolOptions.Count));

            // Add expand / collapse buttons if there are items in the list
            if (poolManager.prefabPoolOptions.Count > 0)
            {
                EZ_EditorUtility.BeginColor(EZ_EditorAssets.shiftPosColor);
                var masterCollapse = GUILayout.Button("Collapse All", EditorStyles.toolbarButton, GUILayout.Width(80));

                var masterExpand = GUILayout.Button("Expand All", EditorStyles.toolbarButton, GUILayout.Width(80));
                EZ_EditorUtility.EndColor();

                if (masterExpand)
                {
                    foreach (var item in poolManager.prefabPoolOptions)
                    {
                        item.isPoolExpanded = true;
                    }
                }

                if (masterCollapse)
                {
                    foreach (var item in poolManager.prefabPoolOptions)
                    {
                        item.isPoolExpanded = false;
                    }
                }
            }
            else
            {
                GUILayout.FlexibleSpace();
            }

            //only enable adding of pools when the application is NOT in play state
            if (!Application.isPlaying) //During Editor
            {
                EZ_EditorUtility.BeginColor(EZ_EditorAssets.addBtnColor);
                if (GUILayout.Button("Add", EditorStyles.toolbarButton, GUILayout.Width(32)))
                {
                    poolManager.prefabPoolOptions.Insert(0, new EZ_PrefabPoolOption()); //add to the top of the list
                    isDirty = true;
                }
                EZ_EditorUtility.EndColor();
            }

            EditorGUILayout.EndHorizontal();

            if (poolManager.isRootExpanded)
            {
                int i_ToRemove    = -1;
                int i_ToInsertAt  = -1;
                int i_ToShiftUp   = -1;
                int i_ToShiftDown = -1;

                EditorGUILayout.BeginVertical();

                scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(0), GUILayout.Height(0));

                for (var i = 0; i < poolManager.prefabPoolOptions.Count; ++i)
                {
                    var prefabPoolOption = poolManager.prefabPoolOptions[i];

                    EditorGUI.indentLevel = 2;
                    EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
                    var name = prefabPoolOption.prefabTransform == null ? "[NO PREFAB]" : prefabPoolOption.prefabTransform.name;
                    if (!Application.isPlaying) //During Editor
                    {
                        prefabPoolOption.isPoolExpanded = EditorGUILayout.Foldout(prefabPoolOption.isPoolExpanded, name, EditorStyles.foldout);
                    }
                    else //During Play
                    {
                        var itemInfo = EZ_PoolManager.GetPool(name);
                        if (itemInfo != null)
                        {
                            var spawnedCount   = itemInfo.spawnedList.Count;
                            var despawnedCount = itemInfo.despawnedList.Count;
                            prefabPoolOption.isPoolExpanded = EditorGUILayout.Foldout(prefabPoolOption.isPoolExpanded, name + " - " + spawnedCount + " / " + (despawnedCount + spawnedCount), EditorStyles.foldout);
                        }
                    }



                    EZ_EditorUtility.BeginColor(EZ_EditorAssets.shiftPosColor);

                    if (i > 0)
                    {
                        // the up arrow.
                        if (GUILayout.Button("▲", EditorStyles.toolbarButton, GUILayout.Width(24)))
                        {
                            i_ToShiftUp = i;
                        }
                    }
                    else
                    {
                        GUILayout.Space(24);
                    }

                    if (i < poolManager.prefabPoolOptions.Count - 1)
                    {
                        // The down arrow will move things towards the end of the List
                        if (GUILayout.Button("▼", EditorStyles.toolbarButton, GUILayout.Width(24)))
                        {
                            i_ToShiftDown = i;
                        }
                    }
                    else
                    {
                        GUILayout.Space(24);
                    }

                    EZ_EditorUtility.EndColor();

                    //only enable adding or deleting of pools when the application is NOT in play state
                    if (!Application.isPlaying) //During Editor
                    {
                        EZ_EditorUtility.BeginColor(EZ_EditorAssets.addBtnColor);
                        if (GUILayout.Button("Add", EditorStyles.toolbarButton, GUILayout.Width(32)))
                        {
                            i_ToInsertAt = i + 1;
                        }
                        EZ_EditorUtility.EndColor();

                        EZ_EditorUtility.BeginColor(EZ_EditorAssets.delBtnColor);
                        if (GUILayout.Button("Del", EditorStyles.toolbarButton, GUILayout.Width(32)))
                        {
                            i_ToRemove = i;
                        }
                        EZ_EditorUtility.EndColor();
                    }

                    EditorGUILayout.EndHorizontal();

                    if (prefabPoolOption.isPoolExpanded)
                    {
                        EditorGUI.indentLevel = 1;

                        EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolItemTop);

                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Space(12); //offset the prefab preview towards the center

                        Texture prefabPreviewIcon = null;
                        if (prefabPoolOption.prefabTransform && prefabPoolOption.prefabTransform.GetComponent <Renderer>() != null)
                        {
                            prefabPreviewIcon = AssetPreview.GetAssetPreview(prefabPoolOption.prefabTransform.gameObject);
                        }
                        else
                        {
                            prefabPreviewIcon = EZ_EditorUtility.LoadTexture(EZ_EditorAssets.missingPrefabIcon);
                        }

                        while (AssetPreview.IsLoadingAssetPreviews())
                        {
                            if (prefabPoolOption.prefabTransform)
                            {
                                prefabPreviewIcon = AssetPreview.GetAssetPreview(prefabPoolOption.prefabTransform.gameObject);
                            }
                            else
                            {
                                prefabPreviewIcon = EZ_EditorUtility.LoadTexture(EZ_EditorAssets.missingPrefabIcon);
                            }

                            System.Threading.Thread.Sleep(5);
                        }

                        EZ_EditorUtility.DrawTexture(prefabPreviewIcon, 100, 100);

                        EditorGUILayout.BeginVertical(GUILayout.MinHeight(prefabPreviewIcon.height));


                        if (!Application.isPlaying) //During Editor
                        {
                            prefabPoolOption.prefabTransform    = (Transform)EditorGUILayout.ObjectField("Prefab", prefabPoolOption.prefabTransform, typeof(Transform), false);
                            prefabPoolOption.showDebugLog       = EditorGUILayout.Toggle("Show Debug Log", prefabPoolOption.showDebugLog);
                            prefabPoolOption.instancesToPreload = EditorGUILayout.IntSlider("Preload Qty", prefabPoolOption.instancesToPreload, 0, 10000);


                            prefabPoolOption.poolCanGrow = EditorGUILayout.Toggle("Allow Pool to grow", prefabPoolOption.poolCanGrow);

                            if (prefabPoolOption.instancesToPreload == 0 && !prefabPoolOption.poolCanGrow)
                            {
                                EditorGUILayout.LabelField("*Preload Qty is 0 and Pool cannot grow. This pool will not be created.", EditorStyles.miniLabel);
                            }

                            if (prefabPoolOption.poolCanGrow)
                            {
                                prefabPoolOption.enableHardLimit = EditorGUILayout.Toggle("Enable Hard Limit", prefabPoolOption.enableHardLimit);

                                if (prefabPoolOption.enableHardLimit)
                                {
                                    prefabPoolOption.hardLimit = EditorGUILayout.IntSlider("Hard Limit", prefabPoolOption.hardLimit, 0, 10000);
                                }
                            }

                            prefabPoolOption.cullDespawned = EditorGUILayout.Toggle("Cull Despawned", prefabPoolOption.cullDespawned);

                            if (prefabPoolOption.cullDespawned)
                            {
                                prefabPoolOption.cullAbove  = EditorGUILayout.IntSlider("Cull Above", prefabPoolOption.cullAbove, 0, 1000);
                                prefabPoolOption.cullDelay  = EditorGUILayout.Slider("Cull Delay", prefabPoolOption.cullDelay, 0, 100);
                                prefabPoolOption.cullAmount = EditorGUILayout.IntSlider("Cull Amt", prefabPoolOption.cullAmount, 0, 100);
                            }

                            prefabPoolOption.recycle = EditorGUILayout.Toggle("Allow Pool to recycle", prefabPoolOption.recycle);
                        }
                        else //During Play mode
                        {
                            if (prefabPoolOption.prefabTransform != null)
                            {
                                var itemInfo = EZ_PoolManager.GetPool(name);
                                if (itemInfo != null)
                                {
                                    var spawnedCount   = itemInfo.spawnedList.Count;
                                    var despawnedCount = itemInfo.despawnedList.Count;

                                    EditorGUILayout.LabelField(string.Format("{0} / {1} Spawned", spawnedCount, despawnedCount + spawnedCount), EditorStyles.boldLabel);
                                    EditorGUILayout.Separator();
                                }
                            }

                            EditorGUILayout.LabelField("Preload Qty : " + prefabPoolOption.instancesToPreload, EditorStyles.miniLabel);
                            EditorGUILayout.LabelField("Allow Pool to grow : " + prefabPoolOption.poolCanGrow, EditorStyles.miniLabel);

                            if (prefabPoolOption.poolCanGrow)
                            {
                                if (prefabPoolOption.enableHardLimit)
                                {
                                    EditorGUILayout.LabelField("Hard Limit : " + prefabPoolOption.hardLimit, EditorStyles.miniLabel);
                                }
                            }

                            if (prefabPoolOption.cullDespawned)
                            {
                                EditorGUILayout.LabelField("Cull Above : " + prefabPoolOption.cullAbove, EditorStyles.miniLabel);
                                EditorGUILayout.LabelField("Cull Delay : " + prefabPoolOption.cullDelay, EditorStyles.miniLabel);
                                EditorGUILayout.LabelField("Cull Amt : " + prefabPoolOption.cullAmount, EditorStyles.miniLabel);
                            }

                            EditorGUILayout.LabelField("Allow Pool to recycle : " + prefabPoolOption.recycle, EditorStyles.miniLabel);
                        }
                        EditorGUILayout.EndVertical();
                        EditorGUILayout.EndHorizontal();

                        EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolItemBottom);
                    }
                }

                EditorGUILayout.EndScrollView();
                EditorGUILayout.EndVertical();

                if (i_ToRemove != -1)
                {
                    poolManager.prefabPoolOptions.RemoveAt(i_ToRemove);
                    isDirty = true;
                }
                if (i_ToInsertAt != -1)
                {
                    poolManager.prefabPoolOptions.Insert(i_ToInsertAt, new EZ_PrefabPoolOption());
                    isDirty = true;
                }
                if (i_ToShiftUp != -1)
                {
                    var item = poolManager.prefabPoolOptions[i_ToShiftUp];
                    poolManager.prefabPoolOptions.Insert(i_ToShiftUp - 1, item);
                    poolManager.prefabPoolOptions.RemoveAt(i_ToShiftUp + 1);
                    isDirty = true;
                }

                if (i_ToShiftDown != -1)
                {
                    var index = i_ToShiftDown + 1;
                    var item  = poolManager.prefabPoolOptions[index];
                    poolManager.prefabPoolOptions.Insert(index - 1, item);
                    poolManager.prefabPoolOptions.RemoveAt(index + 1);
                    isDirty = true;
                }
            }


            if (GUI.changed || isDirty)
            {
                EditorUtility.SetDirty(target);
            }

            this.Repaint();
        }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            EditorGUI.indentLevel = 0;
            GUI.contentColor = Color.white;
            bool isDirty = false;
            poolManager = (EZ_PoolManager)target;

            EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolManagerItemLogo);

            poolManager.autoAddMissingPrefabPool = EditorGUILayout.Toggle("Auto Add Missing Items", poolManager.autoAddMissingPrefabPool);
            poolManager.showDebugLog = EditorGUILayout.Toggle("Show Debug Log", poolManager.showDebugLog);
            poolManager.usePoolManager = EditorGUILayout.Toggle("Use EZ Pool Manager", poolManager.usePoolManager);

            EditorGUILayout.Separator();
            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);

            EditorGUI.indentLevel = 1;

            poolManager.isRootExpanded = EditorGUILayout.Foldout(poolManager.isRootExpanded, string.Format("Pools ({0})", poolManager.prefabPoolOptions.Count));

            // Add expand / collapse buttons if there are items in the list
            if (poolManager.prefabPoolOptions.Count > 0)
            {
                EZ_EditorUtility.BeginColor(EZ_EditorAssets.shiftPosColor);
                var masterCollapse = GUILayout.Button("Collapse All", EditorStyles.toolbarButton, GUILayout.Width(80));

                var masterExpand = GUILayout.Button("Expand All", EditorStyles.toolbarButton, GUILayout.Width(80));
                EZ_EditorUtility.EndColor();

                if (masterExpand)
                {
                    foreach (var item in poolManager.prefabPoolOptions)
                    {
                        item.isPoolExpanded = true;
                    }
                }

                if (masterCollapse)
                {
                    foreach (var item in poolManager.prefabPoolOptions)
                    {
                        item.isPoolExpanded = false;
                    }
                }
            }
            else
            {
                GUILayout.FlexibleSpace();
            }

            //only enable adding of pools when the application is NOT in play state
            if (!Application.isPlaying) //During Editor
            {
                EZ_EditorUtility.BeginColor(EZ_EditorAssets.addBtnColor);
                if (GUILayout.Button("Add", EditorStyles.toolbarButton, GUILayout.Width(32)))
                {
                    poolManager.prefabPoolOptions.Insert(0, new EZ_PrefabPoolOption()); //add to the top of the list
                    isDirty = true;
                }
                EZ_EditorUtility.EndColor();
            }

            EditorGUILayout.EndHorizontal();

            if (poolManager.isRootExpanded)
            {
                int i_ToRemove = -1;
                int i_ToInsertAt = -1;
                int i_ToShiftUp = -1;
                int i_ToShiftDown = -1;

                EditorGUILayout.BeginVertical();

                scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(0), GUILayout.Height(0));

                for (var i = 0; i < poolManager.prefabPoolOptions.Count; ++i)
                {
                    var prefabPoolOption = poolManager.prefabPoolOptions[i];

                    EditorGUI.indentLevel = 2;
                    EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
                    var name = prefabPoolOption.prefabTransform == null ? "[NO PREFAB]" : prefabPoolOption.prefabTransform.name;
                    prefabPoolOption.isPoolExpanded = EditorGUILayout.Foldout(prefabPoolOption.isPoolExpanded, name, EditorStyles.foldout);

                    EZ_EditorUtility.BeginColor(EZ_EditorAssets.shiftPosColor);

                    if (i > 0)
                    {
                        // the up arrow.
                        if (GUILayout.Button("▲", EditorStyles.toolbarButton, GUILayout.Width(24)))
                        {
                            i_ToShiftUp = i;
                        }
                    }
                    else
                    {
                        GUILayout.Space(24);
                    }

                    if (i < poolManager.prefabPoolOptions.Count - 1)
                    {
                        // The down arrow will move things towards the end of the List
                        if (GUILayout.Button("▼", EditorStyles.toolbarButton, GUILayout.Width(24)))
                        {
                            i_ToShiftDown = i;
                        }
                    }
                    else
                    {
                        GUILayout.Space(24);
                    }

                    EZ_EditorUtility.EndColor();

                    //only enable adding or deleting of pools when the application is NOT in play state
                    if (!Application.isPlaying) //During Editor
                    {
                        EZ_EditorUtility.BeginColor(EZ_EditorAssets.addBtnColor);
                        if (GUILayout.Button("Add", EditorStyles.toolbarButton, GUILayout.Width(32)))
                        {
                            i_ToInsertAt = i + 1;
                        }
                        EZ_EditorUtility.EndColor();

                        EZ_EditorUtility.BeginColor(EZ_EditorAssets.delBtnColor);
                        if (GUILayout.Button("Del", EditorStyles.toolbarButton, GUILayout.Width(32)))
                        {
                            i_ToRemove = i;
                        }
                        EZ_EditorUtility.EndColor();
                    }

                    EditorGUILayout.EndHorizontal();

                    if (prefabPoolOption.isPoolExpanded)
                    {
                        EditorGUI.indentLevel = 1;

                        EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolItemTop);

                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Space(12); //offset the prefab preview towards the center

                        Texture prefabPreviewIcon = null;
                        if (prefabPoolOption.prefabTransform && prefabPoolOption.prefabTransform.GetComponent<Renderer>() != null)
                        {
                            prefabPreviewIcon = AssetPreview.GetAssetPreview(prefabPoolOption.prefabTransform.gameObject);
                        }
                        else
                        {
                            prefabPreviewIcon = EZ_EditorUtility.LoadTexture(EZ_EditorAssets.missingPrefabIcon);
                        }

                        while (AssetPreview.IsLoadingAssetPreviews())
                        {
                            if (prefabPoolOption.prefabTransform)
                            {
                                prefabPreviewIcon = AssetPreview.GetAssetPreview(prefabPoolOption.prefabTransform.gameObject);
                            }
                            else
                            {
                                prefabPreviewIcon = EZ_EditorUtility.LoadTexture(EZ_EditorAssets.missingPrefabIcon);
                            }

                            System.Threading.Thread.Sleep(5);
                        }

                        EZ_EditorUtility.DrawTexture(prefabPreviewIcon, 100, 100);

                        EditorGUILayout.BeginVertical(GUILayout.MinHeight(prefabPreviewIcon.height));

                        if (!Application.isPlaying) //During Editor
                        {
                            prefabPoolOption.prefabTransform = (Transform)EditorGUILayout.ObjectField("Prefab", prefabPoolOption.prefabTransform, typeof(Transform), false);
                            prefabPoolOption.showDebugLog = EditorGUILayout.Toggle("Show Debug Log", prefabPoolOption.showDebugLog);
                            prefabPoolOption.instancesToPreload = EditorGUILayout.IntSlider("Preload Qty", prefabPoolOption.instancesToPreload, 0, 10000);

                            prefabPoolOption.poolCanGrow = EditorGUILayout.Toggle("Allow Pool to grow", prefabPoolOption.poolCanGrow);

                            if (prefabPoolOption.instancesToPreload == 0 && !prefabPoolOption.poolCanGrow)
                            {
                                EditorGUILayout.LabelField("*Preload Qty is 0 and Pool cannot grow. This pool will not be created.", EditorStyles.miniLabel);
                            }

                            if (prefabPoolOption.poolCanGrow)
                            {
                                prefabPoolOption.enableHardLimit = EditorGUILayout.Toggle("Enable Hard Limit", prefabPoolOption.enableHardLimit);

                                if (prefabPoolOption.enableHardLimit)
                                {
                                    prefabPoolOption.hardLimit = EditorGUILayout.IntSlider("Hard Limit", prefabPoolOption.hardLimit, 0, 10000);
                                }
                            }

                            prefabPoolOption.cullDespawned = EditorGUILayout.Toggle("Cull Despawned", prefabPoolOption.cullDespawned);

                            if (prefabPoolOption.cullDespawned)
                            {
                                prefabPoolOption.cullAbove = EditorGUILayout.IntSlider("Cull Above", prefabPoolOption.cullAbove, 0, 1000);
                                prefabPoolOption.cullDelay = EditorGUILayout.Slider("Cull Delay", prefabPoolOption.cullDelay, 0, 100);
                                prefabPoolOption.cullAmount = EditorGUILayout.IntSlider("Cull Amt", prefabPoolOption.cullAmount, 0, 100);
                            }

                            prefabPoolOption.recycle = EditorGUILayout.Toggle("Allow Pool to recycle", prefabPoolOption.recycle);
                        }
                        else //During Play mode
                        {
                            if (prefabPoolOption.prefabTransform != null)
                            {
                                var itemInfo = EZ_PoolManager.GetPool(name);
                                if (itemInfo != null)
                                {
                                    var spawnedCount = itemInfo.spawnedList.Count;
                                    var despawnedCount = itemInfo.despawnedList.Count;
                                    EditorGUILayout.LabelField(string.Format("{0} / {1} Spawned", spawnedCount, despawnedCount + spawnedCount), EditorStyles.boldLabel);
                                    EditorGUILayout.Separator();
                                }
                            }

                            EditorGUILayout.LabelField("Preload Qty : " + prefabPoolOption.instancesToPreload, EditorStyles.miniLabel);
                            EditorGUILayout.LabelField("Allow Pool to grow : " + prefabPoolOption.poolCanGrow, EditorStyles.miniLabel);

                            if (prefabPoolOption.poolCanGrow)
                            {
                                if (prefabPoolOption.enableHardLimit)
                                {
                                    EditorGUILayout.LabelField("Hard Limit : " + prefabPoolOption.hardLimit, EditorStyles.miniLabel);
                                }
                            }

                            if (prefabPoolOption.cullDespawned)
                            {
                                EditorGUILayout.LabelField("Cull Above : " + prefabPoolOption.cullAbove, EditorStyles.miniLabel);
                                EditorGUILayout.LabelField("Cull Delay : " + prefabPoolOption.cullDelay, EditorStyles.miniLabel);
                                EditorGUILayout.LabelField("Cull Amt : " + prefabPoolOption.cullAmount, EditorStyles.miniLabel);
                            }

                            EditorGUILayout.LabelField("Allow Pool to recycle : " + prefabPoolOption.recycle, EditorStyles.miniLabel);

                        }
                        EditorGUILayout.EndVertical();
                        EditorGUILayout.EndHorizontal();

                        EZ_EditorUtility.DrawTexture(EZ_EditorAssets.poolItemBottom);
                    }
                }

                EditorGUILayout.EndScrollView();
                EditorGUILayout.EndVertical();

                if (i_ToRemove != -1)
                {
                    poolManager.prefabPoolOptions.RemoveAt(i_ToRemove);
                    isDirty = true;
                }
                if (i_ToInsertAt != -1)
                {
                    poolManager.prefabPoolOptions.Insert(i_ToInsertAt, new EZ_PrefabPoolOption());
                    isDirty = true;
                }
                if (i_ToShiftUp != -1)
                {
                    var item = poolManager.prefabPoolOptions[i_ToShiftUp];
                    poolManager.prefabPoolOptions.Insert(i_ToShiftUp - 1, item);
                    poolManager.prefabPoolOptions.RemoveAt(i_ToShiftUp + 1);
                    isDirty = true;
                }

                if (i_ToShiftDown != -1)
                {
                    var index = i_ToShiftDown + 1;
                    var item = poolManager.prefabPoolOptions[index];
                    poolManager.prefabPoolOptions.Insert(index - 1, item);
                    poolManager.prefabPoolOptions.RemoveAt(index + 1);
                    isDirty = true;
                }
            }

            if (GUI.changed || isDirty)
            {
                EditorUtility.SetDirty(target);
            }

            this.Repaint();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes all the pools set up by user in the editor, and adds them to the Dictionary.
        /// </summary>
        void Awake()
        {
            instance = this;

            parentTransform = this.transform;

            Pools.Clear();
            itemsMarkedForDeletion.Clear();

            if (!usePoolManager)
                return;

            // loop through all the pre-allocated pools and initialize all the pool
            for (var i = 0; i < prefabPoolOptions.Count; ++i)
            {
                var item = prefabPoolOptions[i];
                var prefabTransform = item.prefabTransform;
                var name = prefabTransform.name;

                if (item.instancesToPreload <= 0 && !item.poolCanGrow)
                {
                    itemsMarkedForDeletion.Add(item);
                    continue; // no need to pre-allocate any game obj, nothing else to do
                }

                if (prefabTransform == null)
                {
                    Debug.LogWarning("Item at index " + (i + 1) + " in the Pool has no prefab !");
                    continue;
                }

                if (Pools.ContainsKey(name))
                {
                    Debug.LogWarning("Duplicates found in the Pool : " + name);
                }

                // pre-allocate the game objs
                var tmpList = new List<Transform>();

                for (var j = 0; j < item.instancesToPreload; ++j)
                {
                    var newTransform = GameObject.Instantiate(prefabTransform, Vector3.zero, prefabTransform.rotation) as Transform;
                    newTransform.name = name;
                    newTransform.parent = parentTransform;
                    newTransform.gameObject.SetActive(false);

                    tmpList.Add(newTransform);
                }

                var newPrefabPool = new EZ_PrefabPool(tmpList);
                newPrefabPool.showDebugLog = item.showDebugLog;
                newPrefabPool.poolCanGrow = item.poolCanGrow;
                newPrefabPool.parentTransform = parentTransform;

                newPrefabPool.cullDespawned = item.cullDespawned;
                newPrefabPool.cullAbove = item.cullAbove;
                newPrefabPool.cullDelay = item.cullDelay;
                newPrefabPool.cullAmount = item.cullAmount;

                newPrefabPool.enableHardLimit = item.enableHardLimit;
                newPrefabPool.hardLimit = item.hardLimit;

                newPrefabPool.recycle = item.recycle;

                Pools.Add(name, newPrefabPool); //add the pool to the Dictionary
            }

            foreach (var item in itemsMarkedForDeletion)
            {
                prefabPoolOptions.Remove(item);
            }

            itemsMarkedForDeletion.Clear();
        }