< Summary

Class:DCL.Helpers.GenericFactory
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/GenericFactory.cs
Covered lines:13
Uncovered lines:2
Coverable lines:15
Total lines:56
Line coverage:86.6% (13 of 15)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PrepareCache()0%440100%
Instantiate(...)0%5.935066.67%
Instantiate[T](...)0%220100%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3
 4namespace DCL.Helpers
 5{
 6    [CreateAssetMenu(fileName = "GenericFactory", menuName = "GenericFactory")]
 7    public class GenericFactory : ScriptableObject
 8    {
 9        [System.Serializable]
 10        public class Map
 11        {
 12            public string key;
 13            public GameObject prefab;
 14        }
 15
 16        public GameObject defaultPrefab;
 17        public Map[] items;
 18
 19        private Dictionary<string, GameObject> cachedItems;
 20
 21        private void PrepareCache()
 22        {
 15723            if (cachedItems != null)
 15524                return;
 25
 226            cachedItems = new Dictionary<string, GameObject>();
 3227            for (int i = 0; i < items.Length; i++)
 28            {
 1429                if (!cachedItems.ContainsKey(items[i].key))
 30                {
 1431                    cachedItems.Add(items[i].key, items[i].prefab);
 32                }
 33            }
 234        }
 35
 36        public GameObject Instantiate(string key, Transform parent = null)
 37        {
 15738            PrepareCache();
 39
 15740            GameObject prefab = cachedItems.ContainsKey(key) ? cachedItems[key] : defaultPrefab;
 15741            if (prefab == null)
 42            {
 043                Debug.LogError($"Prefab for Key: {key} is null!");
 044                return default;
 45            }
 46
 15747            return parent == null ? Instantiate(prefab) : Instantiate(prefab, parent);
 48        }
 49
 50        public T Instantiate<T>(string key, Transform parent = null) where T : Component
 51        {
 15752            var prefab = Instantiate(key, parent);
 15753            return prefab == null ? null : prefab.GetComponent<T>();
 54        }
 55    }
 56}