< Summary

Class:BIWOutline
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/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/Scripts/MainScripts/DCL/Controllers/BuilderMode/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    {
 1325        builderCamera = GetComponent<Camera>();
 26
 1327        outlineCamera = new GameObject("BuilderOutlineCamera").AddComponent<Camera>();
 1328        outlineCamera.CopyFrom(builderCamera);
 1329        outlineCamera.clearFlags = CameraClearFlags.SolidColor;
 1330        outlineCamera.backgroundColor = Color.clear;
 1331        outlineCamera.cullingMask = BIWSettings.SELECTION_LAYER;
 1332        outlineCamera.depth = builderCamera.depth - 1;
 1333        outlineCamera.transform.SetParent(builderCamera.transform);
 34
 1335        outlineCanvas = new GameObject("BuilderOutlineCanvas").AddComponent<Canvas>();
 1336        outlineCanvas.renderMode = RenderMode.ScreenSpaceCamera;
 1337        outlineCanvas.worldCamera = builderCamera;
 1338        outlineCanvas.planeDistance = 1;
 39
 1340        outlineRawImage = new GameObject("BuilderOutlineRawImage").AddComponent<RawImage>();
 1341        outlineRawImage.transform.SetParent(outlineCanvas.transform);
 1342        outlineRawImage.transform.ResetLocalTRS();
 43
 1344        outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei
 1345        outlineRawImage.raycastTarget = false;
 1346        outlineRawImage.material = OutlineMaterial;
 1347    }
 48
 2649    private void OnDestroy() { Dispose(); }
 50
 51    public void Dispose()
 52    {
 2653        if (outlineCanvas != null)
 2054            Destroy(outlineCanvas.gameObject);
 2655        if (outlineRawImage != null)
 2056            Destroy(outlineRawImage.gameObject);
 2657        if (outlineCamera != null)
 2058            Destroy(outlineCamera.gameObject);
 2659    }
 60
 61    public void Activate()
 62    {
 1463        outlineCamera.gameObject.SetActive(true);
 1464        outlineCanvas.gameObject.SetActive(true);
 1465        outlineRawImage.gameObject.SetActive(true);
 1466    }
 67
 68    public void Deactivate()
 69    {
 970        outlineCamera.gameObject.SetActive(false);
 971        outlineCanvas.gameObject.SetActive(false);
 972        outlineRawImage.gameObject.SetActive(false);
 973    }
 74
 75    public void SetOutlineMaterial(Material mat)
 76    {
 1377        OutlineMaterial = mat;
 1378        outlineRawImage.material = OutlineMaterial;
 1379    }
 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}