< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%2100%
OnDestroy()0%2100%
Dispose()0%20400%
Activate()0%2100%
Deactivate()0%2100%
SetOutlineMaterial(...)0%2100%
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    {
 025        builderCamera = GetComponent<Camera>();
 26
 027        outlineCamera = new GameObject("BuilderOutlineCamera").AddComponent<Camera>();
 028        outlineCamera.CopyFrom(builderCamera);
 029        outlineCamera.clearFlags = CameraClearFlags.SolidColor;
 030        outlineCamera.backgroundColor = Color.clear;
 031        outlineCamera.cullingMask = BIWSettings.SELECTION_LAYER;
 032        outlineCamera.depth = builderCamera.depth - 1;
 033        outlineCamera.transform.SetParent(builderCamera.transform);
 34
 035        outlineCanvas = new GameObject("BuilderOutlineCanvas").AddComponent<Canvas>();
 036        outlineCanvas.renderMode = RenderMode.ScreenSpaceCamera;
 037        outlineCanvas.worldCamera = builderCamera;
 038        outlineCanvas.planeDistance = 1;
 39
 040        outlineRawImage = new GameObject("BuilderOutlineRawImage").AddComponent<RawImage>();
 041        outlineRawImage.transform.SetParent(outlineCanvas.transform);
 042        outlineRawImage.transform.ResetLocalTRS();
 43
 044        outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei
 045        outlineRawImage.raycastTarget = false;
 046        outlineRawImage.material = OutlineMaterial;
 047    }
 48
 049    private void OnDestroy() { Dispose(); }
 50
 51    public void Dispose()
 52    {
 053        if (outlineCanvas != null)
 054            Destroy(outlineCanvas.gameObject);
 055        if (outlineRawImage != null)
 056            Destroy(outlineRawImage.gameObject);
 057        if (outlineCamera != null)
 058            Destroy(outlineCamera.gameObject);
 059    }
 60
 61    public void Activate()
 62    {
 063        outlineCamera.gameObject.SetActive(true);
 064        outlineCanvas.gameObject.SetActive(true);
 065        outlineRawImage.gameObject.SetActive(true);
 066    }
 67
 68    public void Deactivate()
 69    {
 070        outlineCamera.gameObject.SetActive(false);
 071        outlineCanvas.gameObject.SetActive(false);
 072        outlineRawImage.gameObject.SetActive(false);
 073    }
 74
 75    public void SetOutlineMaterial(Material mat)
 76    {
 077        OutlineMaterial = mat;
 078        outlineRawImage.material = OutlineMaterial;
 079    }
 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}