< Summary

Class:GDX.Collections.Pooling.ManagedPoolBuilder
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPoolBuilder.cs
Covered lines:22
Uncovered lines:40
Coverable lines:62
Total lines:164
Line coverage:35.4% (22 of 62)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:5
Method coverage:60% (3 of 5)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ManagedPoolBuilder()0%110100%
LateUpdate()0%6200%
AddManagedPool(...)0%3.333066.67%
RemoveManagedPool(...)0%6.475061.11%
Tick()0%30500%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPoolBuilder.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !UNITY_DOTSRUNTIME
 6
 7using System.Collections.Generic;
 8using UnityEngine;
 9
 10namespace GDX.Collections.Pooling
 11{
 12    /// <summary>
 13    ///     A time-slicing builder behaviour for <see cref="IManagedPool" />.
 14    /// </summary>
 15    /// <remarks>A demonstration of usage can be found in <see cref="GameObjectPool" />.</remarks>
 16    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 17    public class ManagedPoolBuilder : MonoBehaviour
 18    {
 19        /// <summary>
 20        ///     The number of instantiates to make per frame.
 21        /// </summary>
 22        /// <remarks>
 23        ///     During defined loading periods this value could be increased for faster allocations,
 24        ///     and then returned to a much more performant value afterwards.
 25        /// </remarks>
 126        public static int InstantiatesPerFrame = 5;
 27
 28        /// <summary>
 29        ///     Should the <see cref="ManagedPoolBuilder" /> destroy itself when finished?
 30        /// </summary>
 131        public static bool DestroyBuilderOnFinish = true;
 32
 33        /// <summary>
 34        ///     A cached reference to the <see cref="GameObject" /> the builder created for itself.
 35        /// </summary>
 36        static GameObject s_BuilderObject;
 37
 38        /// <summary>
 39        ///     A <see cref="List{T}" /> of <see cref="IManagedPool" /> which are being built out.
 40        /// </summary>
 141        static readonly List<IManagedPool> k_TargetPools = new List<IManagedPool>();
 42
 43        /// <summary>
 44        ///     A cached numerical count of the number of <see cref="IManagedPool" /> contained in <see cref="k_TargetPo
 45        /// </summary>
 46        static int s_TargetPoolsCount;
 47
 48        /// <summary>
 49        ///     Unity's LateUpdate event.
 50        /// </summary>
 51#pragma warning disable IDE0051
 52        // ReSharper disable UnusedMember.Local
 53        void LateUpdate()
 054        {
 55            // ReSharper disable once Unity.PerformanceCriticalCodeInvocation, Unity.ExpensiveCode
 056            Tick();
 57
 58            // Do we have work to be done?
 059            if (s_TargetPoolsCount != 0)
 060            {
 061                return;
 62            }
 63
 64            // Nothing left to do, get rid of myself
 065            s_BuilderObject = null;
 066            Destroy(gameObject);
 067        }
 68        // ReSharper restore UnusedMember.Local
 69#pragma warning restore IDE0051
 70
 71        /// <summary>
 72        ///     Add an <see cref="IManagedPool" /> to the builder to be built out.
 73        /// </summary>
 74        /// <param name="targetManagedPool">The <see cref="IManagedPool" /> to build out.</param>
 75        public static void AddManagedPool(IManagedPool targetManagedPool)
 1476        {
 1477            if (k_TargetPools.Contains(targetManagedPool))
 078            {
 079                return;
 80            }
 81
 1482            k_TargetPools.Add(targetManagedPool);
 1483            s_TargetPoolsCount++;
 84
 85            // We already have a builder, no need to make one.
 1486            if (s_BuilderObject != null)
 087            {
 088                return;
 89            }
 90
 1491            s_BuilderObject = new GameObject("GDX.GameObjectPoolBuilder");
 1492            s_BuilderObject.AddComponent<ManagedPoolBuilder>();
 1493        }
 94
 95        /// <summary>
 96        ///     Remove an <see cref="IManagedPool" /> from the builder.
 97        /// </summary>
 98        /// <param name="targetManagedPool">The <see cref="IManagedPool" /> to be removed.</param>
 99        public static void RemoveManagedPool(IManagedPool targetManagedPool)
 14100        {
 14101            if (!k_TargetPools.Contains(targetManagedPool))
 0102            {
 0103                return;
 104            }
 105
 14106            k_TargetPools.Remove(targetManagedPool);
 14107            s_TargetPoolsCount--;
 108
 109            // We still have pools, no sense destroying anything yet
 14110            if (s_TargetPoolsCount > 0 || !DestroyBuilderOnFinish)
 0111            {
 0112                return;
 113            }
 114#if UNITY_EDITOR
 14115            if (Application.isPlaying)
 0116            {
 0117                Destroy(s_BuilderObject);
 0118            }
 119            else
 14120            {
 14121                DestroyImmediate(s_BuilderObject);
 14122            }
 123#else
 124            Destroy(s_BuilderObject);
 125#endif
 14126            s_BuilderObject = null;
 14127        }
 128
 129        /// <summary>
 130        ///     Extracted tick update for the builder; creating a limited number of items per tick.
 131        /// </summary>
 132        static void Tick()
 0133        {
 0134            int spawnsThisUpdate = 0;
 135
 0136            for (int i = s_TargetPoolsCount - 1; i >= 0; i--)
 0137            {
 0138                if (k_TargetPools[i] == null)
 0139                {
 0140                    continue;
 141                }
 142
 0143                if (k_TargetPools[i].HasMinimumPooledItems())
 0144                {
 0145                    k_TargetPools.RemoveAt(i);
 0146                    s_TargetPoolsCount--;
 0147                }
 148                else
 0149                {
 150                    // Build Item
 151                    // ReSharper disable once Unity.PerformanceCriticalCodeInvocation
 0152                    k_TargetPools[i].CreateItem();
 153
 0154                    spawnsThisUpdate++;
 0155                    if (spawnsThisUpdate > InstantiatesPerFrame)
 0156                    {
 0157                        break;
 158                    }
 0159                }
 0160            }
 0161        }
 162    }
 163}
 164#endif