< Summary

Class:DCL.Components.OnPointerEventColliders
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/OnPointerEventColliders.cs
Covered lines:58
Uncovered lines:3
Coverable lines:61
Total lines:143
Line coverage:95% (58 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OnPointerEventColliders()0%110100%
GetMeshName(...)0%220100%
Initialize(...)0%13.0213095%
UpdateCollidersWithRenderersMesh(...)0%330100%
CreateCollider(...)0%220100%
AreCollidersCreated(...)0%8.18088.24%
Dispose()0%110100%
DestroyColliders()0%440100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/OnPointerEventColliders.cs

#LineLine coverage
 1using System;
 2using DCL.Configuration;
 3using DCL.Helpers;
 4using DCL.Models;
 5using System.Collections.Generic;
 6using UnityEngine;
 7
 8namespace DCL.Components
 9{
 10    public class OnPointerEventColliders : IDisposable
 11    {
 12        public const string COLLIDER_NAME = "OnPointerEventCollider";
 13
 14        internal MeshCollider[] colliders;
 5315        Dictionary<Collider, string> colliderNames = new Dictionary<Collider, string>();
 16
 17        public string GetMeshName(Collider collider)
 18        {
 619            if (colliderNames.ContainsKey(collider))
 320                return colliderNames[collider];
 21
 322            return null;
 23        }
 24
 25        private IDCLEntity ownerEntity;
 26
 27        public void Initialize(IDCLEntity entity)
 28        {
 10329            Renderer[] rendererList = entity?.meshesInfo?.renderers;
 30
 10331            if (rendererList == null || rendererList.Length == 0)
 32            {
 2533                if (colliders != null && colliders.Length > 0)
 734                    DestroyColliders();
 35
 2536                return;
 37            }
 38
 7839            if (AreCollidersCreated(rendererList))
 40            {
 2141                UpdateCollidersWithRenderersMesh(rendererList);
 2142                return;
 43            }
 44
 5745            IShape shape = entity.meshesInfo.currentShape;
 46
 5747            ownerEntity = entity;
 48
 5749            DestroyColliders();
 50
 5751            if (shape == null)
 052                return;
 53
 5754            colliders = new MeshCollider[rendererList.Length];
 55
 29256            for (int i = 0; i < colliders.Length; i++)
 57            {
 8958                if (rendererList[i] == null)
 59                    continue;
 8960                colliders[i] = CreateCollider(rendererList[i]);
 61            }
 5762        }
 63
 64        void UpdateCollidersWithRenderersMesh(Renderer[] rendererList)
 65        {
 9266            for (int i = 0; i < colliders.Length; i++)
 67            {
 2568                if(colliders[i].sharedMaterial == null)
 2569                    colliders[i].sharedMesh = rendererList[i].GetComponent<MeshFilter>().sharedMesh;
 70            }
 2171        }
 72
 73        MeshCollider CreateCollider(Renderer renderer)
 74        {
 8975            GameObject go = new GameObject(COLLIDER_NAME);
 76
 8977            go.name = COLLIDER_NAME;
 8978            go.layer = PhysicsLayers.onPointerEventLayer; // to avoid character collisions with onclick collider
 79
 8980            var meshCollider = go.AddComponent<MeshCollider>();
 8981            meshCollider.sharedMesh = renderer.GetComponent<MeshFilter>().sharedMesh;
 8982            meshCollider.enabled = renderer.enabled;
 83
 8984            if (renderer.transform.parent != null)
 8985                colliderNames.Add(meshCollider, renderer.transform.parent.name);
 86
 8987            CollidersManager.i.AddOrUpdateEntityCollider(ownerEntity, meshCollider);
 88
 89            // Reset objects position, rotation and scale once it's been parented
 8990            Transform t = go.transform;
 91
 8992            t.SetParent(renderer.transform);
 8993            t.ResetLocalTRS();
 94
 8995            return meshCollider;
 96        }
 97
 98        private bool AreCollidersCreated(Renderer[] rendererList)
 99        {
 78100            if (colliders == null || colliders.Length == 0)
 57101                return false;
 102
 21103            if (rendererList.Length != colliders.Length)
 0104                return false;
 105
 92106            for (int i = 0; i < rendererList.Length; i++)
 107            {
 25108                bool foundChildCollider = false;
 62109                for (int j = 0; j < colliders.Length; j++)
 110                {
 31111                    if (colliders[j].transform.parent == rendererList[i].transform)
 112                    {
 25113                        foundChildCollider = true;
 25114                        break;
 115                    }
 116                }
 117
 25118                if (!foundChildCollider)
 0119                    return false;
 120            }
 121
 21122            return true;
 123        }
 124
 106125        public void Dispose() { DestroyColliders(); }
 126
 127        void DestroyColliders()
 128        {
 117129            if (colliders == null)
 60130                return;
 131
 292132            for (int i = 0; i < colliders.Length; i++)
 133            {
 89134                Collider collider = colliders[i];
 135
 89136                if (collider != null)
 47137                    UnityEngine.Object.Destroy(collider.gameObject);
 138            }
 139
 57140            colliders = null;
 57141        }
 142    }
 143}