< Summary

Class:Builder.DCLBuilderOutline
Assembly:Builder
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderOutline.cs
Covered lines:46
Uncovered lines:14
Coverable lines:60
Total lines:115
Line coverage:76.6% (46 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnDestroy()0%440100%
Activate()0%2100%
Deactivate()0%2100%
SetOutlineMaterial(...)0%2100%
OnResize()0%2.012086.67%
OnPreviewModeChanged(...)0%2100%
Update()0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Builder/Scripts/DCLBuilderOutline.cs

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.UI;
 3using DCL.Helpers;
 4
 5namespace Builder
 6{
 7    [RequireComponent(typeof(Camera))]
 8    public class DCLBuilderOutline : MonoBehaviour
 9    {
 10        [SerializeField] Material OutlineMaterial = null;
 11
 12        private Camera builderCamera;
 13
 14        private Camera outlineCamera;
 15        private Canvas outlineCanvas;
 16        private RawImage outlineRawImage;
 17
 18        private int lastScreenWidth = 0;
 19        private int lastScreenHeight = 0;
 20
 21        private void Awake()
 22        {
 123            builderCamera = GetComponent<Camera>();
 24
 125            outlineCamera = new GameObject("BuilderOutlineCamera").AddComponent<Camera>();
 126            outlineCamera.CopyFrom(builderCamera);
 127            outlineCamera.clearFlags = CameraClearFlags.SolidColor;
 128            outlineCamera.backgroundColor = Color.clear;
 129            outlineCamera.cullingMask = LayerMask.GetMask(DCLBuilderRaycast.LAYER_SELECTION);
 130            outlineCamera.depth = builderCamera.depth - 1;
 131            outlineCamera.transform.SetParent(builderCamera.transform);
 32
 133            outlineCanvas = new GameObject("BuilderOutlineCanvas").AddComponent<Canvas>();
 134            outlineCanvas.renderMode = RenderMode.ScreenSpaceCamera;
 135            outlineCanvas.worldCamera = builderCamera;
 136            outlineCanvas.planeDistance = 1;
 37
 138            outlineRawImage = new GameObject("BuilderOutlineRawImage").AddComponent<RawImage>();
 139            outlineRawImage.transform.SetParent(outlineCanvas.transform);
 140            outlineRawImage.transform.ResetLocalTRS();
 41
 142            outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect
 143            outlineRawImage.raycastTarget = false;
 144            outlineRawImage.material = OutlineMaterial;
 45
 146            DCLBuilderBridge.OnPreviewModeChanged += OnPreviewModeChanged;
 147        }
 48
 49        private void OnDestroy()
 50        {
 151            if (outlineCanvas != null)
 152                Destroy(outlineCanvas.gameObject);
 153            if (outlineRawImage != null)
 154                Destroy(outlineRawImage.gameObject);
 155            if (outlineCamera != null)
 156                Destroy(outlineCamera.gameObject);
 157            DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged;
 158        }
 59
 60        public void Activate()
 61        {
 062            outlineCamera.gameObject.SetActive(true);
 063            outlineCanvas.gameObject.SetActive(true);
 064            outlineRawImage.gameObject.SetActive(true);
 065        }
 66
 67        public void Deactivate()
 68        {
 069            outlineCamera.gameObject.SetActive(false);
 070            outlineCanvas.gameObject.SetActive(false);
 071            outlineRawImage.gameObject.SetActive(false);
 072        }
 73
 74        public void SetOutlineMaterial(Material mat)
 75        {
 076            OutlineMaterial = mat;
 077            outlineRawImage.material = OutlineMaterial;
 078        }
 79
 80        private void OnResize()
 81        {
 182            outlineRawImage.gameObject.SetActive(false); // Hack: force Unity to refresh the texture
 83
 184            RenderTexture currentRenderTexture = outlineCamera.targetTexture;
 185            if (currentRenderTexture != null)
 86            {
 087                currentRenderTexture.Release();
 088                Object.Destroy(currentRenderTexture);
 89            }
 90
 191            RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
 192            renderTexture.antiAliasing = 4;
 193            renderTexture.depth = 16;
 194            renderTexture.autoGenerateMips = true;
 195            renderTexture.useMipMap = true;
 96
 197            outlineCamera.targetTexture = renderTexture;
 198            OutlineMaterial.mainTexture = outlineCamera.targetTexture;
 199            outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect
 1100            outlineRawImage.gameObject.SetActive(true);
 1101        }
 102
 0103        private void OnPreviewModeChanged(bool isPreview) { outlineCanvas.gameObject.SetActive(!isPreview); }
 104
 105        private void Update()
 106        {
 11107            if ((Screen.width > 0 && Screen.height > 0) && (lastScreenWidth != Screen.width || lastScreenHeight != Scree
 108            {
 1109                lastScreenWidth = Screen.width;
 1110                lastScreenHeight = Screen.height;
 1111                OnResize();
 112            }
 11113        }
 114    }
 115}