< Summary

Class:MainScripts.DCL.Helpers.Utils.UnityObjectPool[T]
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/UnityObjectPool.cs
Covered lines:33
Uncovered lines:2
Coverable lines:35
Total lines:121
Line coverage:94.2% (33 of 35)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:13
Method coverage:84.6% (11 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UnityObjectPool(...)0%440100%
MainScripts.DCL.Helpers.Utils.IUnityObjectPool<T>.get_Prefab()0%110100%
Prewarm(...)0%220100%
PrewarmAsync()0%660100%
Instantiate()0%220100%
DisableObject(...)0%110100%
EnableObject(...)0%110100%
Destroy(...)0%220100%
Get()0%110100%
Get()0%2100%
Release(...)0%110100%
Clear()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/UnityObjectPool.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Threading;
 4using UnityEngine;
 5using UnityEngine.Pool;
 6using Object = UnityEngine.Object;
 7
 8namespace MainScripts.DCL.Helpers.Utils
 9{
 10    public interface IUnityObjectPool<T> : IObjectPool<T> where T : Component
 11    {
 12        T Prefab { get; }
 13
 14        void Prewarm(int count);
 15
 16        UniTask PrewarmAsync(int count, int createPerFrame, CancellationToken ct);
 17    }
 18
 19    public class UnityObjectPool<T> : IUnityObjectPool<T> where T: Component
 20    {
 21        private readonly T prefab;
 22        private readonly Transform parent;
 23        private readonly Vector3 defaultPosition;
 24        private readonly Quaternion defaultRot;
 25        private readonly Action<T> actionOnCreate;
 26
 27        private readonly ObjectPool<T> internalPool;
 28
 85229        public UnityObjectPool(
 30            T prefab,
 31            Transform parent,
 32            Vector3 defaultPosition = default,
 33            Quaternion defaultRot = default,
 34            Action<T> actionOnCreate = null,
 35            Action<T> actionOnGet = null,
 36            Action<T> actionOnRelease = null,
 37            Action<T> actionOnDestroy = null,
 38            int defaultCapacity = 10,
 39            int maxSize = 1000)
 40        {
 85241            this.prefab = prefab;
 85242            this.parent = parent;
 85243            this.defaultPosition = defaultPosition;
 85244            this.defaultRot = defaultRot;
 85245            this.actionOnCreate = actionOnCreate;
 46
 85247            internalPool = new ObjectPool<T>(
 48                Instantiate,
 49                actionOnGet ?? EnableObject,
 50                actionOnRelease ?? DisableObject,
 51                actionOnDestroy ?? Destroy,
 52                Application.isEditor,
 53                defaultCapacity,
 54                maxSize
 55            );
 85256        }
 57
 576058        T IUnityObjectPool<T>.Prefab => prefab;
 59
 60        public void Prewarm(int count)
 61        {
 654262            for (var i = 0; i < count; i++)
 280363                Release(Instantiate());
 46864        }
 65
 66        public async UniTask PrewarmAsync(int count, int createPerFrame, CancellationToken ct)
 67        {
 128068            while (count > 0)
 69            {
 3525270                for (var i = 0; i < createPerFrame && count > 0; i++)
 71                {
 1670072                    Release(Instantiate());
 1670073                    count--;
 74                }
 75
 92676                await UniTask.DelayFrame(1, cancellationToken: ct);
 77            }
 35478        }
 79
 80        private T Instantiate()
 81        {
 1950382            var obj = Object.Instantiate(prefab, defaultPosition, defaultRot, parent);
 1950383            actionOnCreate?.Invoke(obj);
 1950384            return obj;
 85        }
 86
 87        private static void DisableObject(T obj)
 88        {
 1953789            obj.gameObject.SetActive(false);
 1953490        }
 91
 92        private static void EnableObject(T obj)
 93        {
 2694            obj.gameObject.SetActive(true);
 2695        }
 96
 97        private void Destroy(T obj)
 98        {
 129599            if (obj)
 1295100                global::DCL.Helpers.Utils.SafeDestroy(obj.gameObject);
 1295101        }
 102
 103        public T Get() =>
 26104            internalPool.Get();
 105
 106        public PooledObject<T> Get(out T v) =>
 0107            internalPool.Get(out v);
 108
 109        public void Release(T element)
 110        {
 19537111            internalPool.Release(element);
 19534112        }
 113
 114        public void Clear()
 115        {
 85116            internalPool.Clear();
 85117        }
 118
 0119        public int CountInactive => internalPool.CountInactive;
 120    }
 121}