< Summary

Class:AvatarName
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarName/AvatarName.cs
Covered lines:11
Uncovered lines:31
Coverable lines:42
Total lines:101
Line coverage:26.1% (11 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarName()0%110100%
SetName(...)0%6.744044.44%
SetTalking(...)0%20400%
Awake()0%220100%
OnEnable()0%110100%
LateUpdate()0%20400%
RefreshTextPosition()0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarName/AvatarName.cs

#LineLine coverage
 1using DCL.Helpers;
 2using TMPro;
 3using UnityEngine;
 4
 5public class AvatarName : MonoBehaviour
 6{
 7    const float NAME_VANISHING_POINT_DISTANCE = 20.0f;
 5678    readonly int VOICE_CHAT_ANIMATOR_TALKING = Animator.StringToHash("Talking");
 9
 10    public CanvasGroup uiContainer;
 11    public Transform sourceTransform;
 12    public TextMeshProUGUI nameText;
 13    public Vector3 offset;
 14    public Animator talkingAnimator;
 15    Canvas canvas;
 16    Camera mainCamera;
 17    RectTransform canvasRect;
 18
 19    Vector2 res;
 20
 21    public void SetName(string name)
 22    {
 123        talkingAnimator?.gameObject.SetActive(false);
 124        if (string.IsNullOrEmpty(name))
 25        {
 126            uiContainer.alpha = 0;
 127            return;
 28        }
 29
 030        if (nameText.text != name)
 31        {
 032            nameText.text = name;
 033            Utils.ForceRebuildLayoutImmediate(canvasRect);
 034            RefreshTextPosition();
 35        }
 036    }
 37
 38    public void SetTalking(bool talking)
 39    {
 040        if (!talkingAnimator)
 041            return;
 42
 043        if (talking && !talkingAnimator.gameObject.activeSelf)
 44        {
 045            talkingAnimator.gameObject.SetActive(talking);
 46        }
 047        talkingAnimator.SetBool(VOICE_CHAT_ANIMATOR_TALKING, talking);
 048    }
 49
 50    private void Awake()
 51    {
 152        canvas = GetComponentInParent<Canvas>();
 153        canvasRect = (RectTransform)canvas.transform;
 154        talkingAnimator?.gameObject.SetActive(false);
 155    }
 56
 57    void OnEnable()
 58    {
 59        // We initialize mainCamera here because the main camera may change while the gameobject is disabled
 260        mainCamera = Camera.main;
 261    }
 62
 63    void LateUpdate()
 64    {
 065        if (string.IsNullOrEmpty(nameText.text))
 066            return;
 67
 068        RefreshTextPosition();
 69
 070        if (Screen.width != res.x || Screen.height != res.y)
 071            nameText.SetAllDirty();
 72
 073        res = new Vector2(Screen.width, Screen.height);
 074    }
 75
 76    private void RefreshTextPosition()
 77    {
 078        Vector3 screenPoint = mainCamera == null ? Vector3.zero : mainCamera.WorldToViewportPoint(sourceTransform.positi
 079        uiContainer.alpha = 1.0f + (1.0f - (screenPoint.z / NAME_VANISHING_POINT_DISTANCE));
 80
 081        if (screenPoint.z > 0)
 82        {
 083            if (!uiContainer.gameObject.activeSelf)
 84            {
 085                uiContainer.gameObject.SetActive(true);
 86            }
 87
 088            float width = canvasRect.rect.width;
 089            float height = canvasRect.rect.height;
 090            screenPoint.Scale(new Vector3(width, height, 0));
 091            ((RectTransform)transform).anchoredPosition = screenPoint;
 092        }
 93        else
 94        {
 095            if (uiContainer.gameObject.activeSelf)
 96            {
 097                uiContainer.gameObject.SetActive(false);
 98            }
 99        }
 0100    }
 101}