< Summary

Class:BIWOutline
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BIWOutline.cs
Covered lines:38
Uncovered lines:21
Coverable lines:59
Total lines:116
Line coverage:64.4% (38 of 59)
Covered branches:0
Total branches:0

Metrics

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

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BIWOutline.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Configuration;
 4using DCL.Helpers;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8[RequireComponent(typeof(Camera))]
 9public class BIWOutline : MonoBehaviour
 10{
 11
 12    [SerializeField] Material OutlineMaterial = null;
 13
 14    private Camera builderCamera;
 15
 16    private Camera outlineCamera;
 17    private Canvas outlineCanvas;
 18    private RawImage outlineRawImage;
 19
 20    private int lastScreenWidth = 0;
 21    private int lastScreenHeight = 0;
 22
 23    private void Awake()
 24    {
 625        builderCamera = GetComponent<Camera>();
 26
 627        outlineCamera = new GameObject("BuilderOutlineCamera").AddComponent<Camera>();
 628        outlineCamera.CopyFrom(builderCamera);
 629        outlineCamera.clearFlags = CameraClearFlags.SolidColor;
 630        outlineCamera.backgroundColor = Color.clear;
 631        outlineCamera.cullingMask = BIWSettings.SELECTION_LAYER;
 632        outlineCamera.depth = builderCamera.depth - 1;
 633        outlineCamera.transform.SetParent(builderCamera.transform);
 34
 635        outlineCanvas = new GameObject("BuilderOutlineCanvas").AddComponent<Canvas>();
 636        outlineCanvas.renderMode = RenderMode.ScreenSpaceCamera;
 637        outlineCanvas.worldCamera = builderCamera;
 638        outlineCanvas.planeDistance = 1;
 39
 640        outlineRawImage = new GameObject("BuilderOutlineRawImage").AddComponent<RawImage>();
 641        outlineRawImage.transform.SetParent(outlineCanvas.transform);
 642        outlineRawImage.transform.ResetLocalTRS();
 43
 644        outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei
 645        outlineRawImage.raycastTarget = false;
 646        outlineRawImage.material = OutlineMaterial;
 647    }
 48
 1249    private void OnDestroy() { Dispose(); }
 50
 51    public void Dispose()
 52    {
 1253        if (outlineCanvas != null)
 654            Destroy(outlineCanvas.gameObject);
 1255        if (outlineRawImage != null)
 656            Destroy(outlineRawImage.gameObject);
 1257        if (outlineCamera != null)
 658            Destroy(outlineCamera.gameObject);
 1259    }
 60
 61    public void Activate()
 62    {
 763        outlineCamera.gameObject.SetActive(true);
 764        outlineCanvas.gameObject.SetActive(true);
 765        outlineRawImage.gameObject.SetActive(true);
 766    }
 67
 68    public void Deactivate()
 69    {
 170        outlineCamera.gameObject.SetActive(false);
 171        outlineCanvas.gameObject.SetActive(false);
 172        outlineRawImage.gameObject.SetActive(false);
 173    }
 74
 75    public void SetOutlineMaterial(Material mat)
 76    {
 677        OutlineMaterial = mat;
 678        outlineRawImage.material = OutlineMaterial;
 679    }
 80
 81    private void OnResize()
 82    {
 083        outlineRawImage.gameObject.SetActive(false); // Hack: force Unity to refresh the texture
 84
 085        RenderTexture currentRenderTexture = outlineCamera.targetTexture;
 086        if (currentRenderTexture != null)
 87        {
 088            currentRenderTexture.Release();
 089            Object.Destroy(currentRenderTexture);
 90        }
 91
 092        RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
 093        renderTexture.antiAliasing = 4;
 094        renderTexture.depth = 16;
 095        renderTexture.autoGenerateMips = true;
 096        renderTexture.useMipMap = true;
 97
 098        outlineCamera.targetTexture = renderTexture;
 099        OutlineMaterial.mainTexture = outlineCamera.targetTexture;
 0100        outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei
 0101        outlineRawImage.gameObject.SetActive(true);
 0102    }
 103
 0104    private void OnPreviewModeChanged(bool isPreview) { outlineCanvas.gameObject.SetActive(!isPreview); }
 105
 106    private void Update()
 107    {
 0108        if ((Screen.width > 0 && Screen.height > 0) && (lastScreenWidth != Screen.width || lastScreenHeight != Screen.he
 109        {
 0110            lastScreenWidth = Screen.width;
 0111            lastScreenHeight = Screen.height;
 0112            OnResize();
 113        }
 0114    }
 115
 116}