| | 1 | | using DCL.Helpers; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class AvatarName : MonoBehaviour |
| | 6 | | { |
| | 7 | | const float NAME_VANISHING_POINT_DISTANCE = 20.0f; |
| 567 | 8 | | 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 | | { |
| 1 | 23 | | talkingAnimator?.gameObject.SetActive(false); |
| 1 | 24 | | if (string.IsNullOrEmpty(name)) |
| | 25 | | { |
| 1 | 26 | | uiContainer.alpha = 0; |
| 1 | 27 | | return; |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | if (nameText.text != name) |
| | 31 | | { |
| 0 | 32 | | nameText.text = name; |
| 0 | 33 | | Utils.ForceRebuildLayoutImmediate(canvasRect); |
| 0 | 34 | | RefreshTextPosition(); |
| | 35 | | } |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void SetTalking(bool talking) |
| | 39 | | { |
| 0 | 40 | | if (!talkingAnimator) |
| 0 | 41 | | return; |
| | 42 | |
|
| 0 | 43 | | if (talking && !talkingAnimator.gameObject.activeSelf) |
| | 44 | | { |
| 0 | 45 | | talkingAnimator.gameObject.SetActive(talking); |
| | 46 | | } |
| 0 | 47 | | talkingAnimator.SetBool(VOICE_CHAT_ANIMATOR_TALKING, talking); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | private void Awake() |
| | 51 | | { |
| 1 | 52 | | canvas = GetComponentInParent<Canvas>(); |
| 1 | 53 | | canvasRect = (RectTransform)canvas.transform; |
| 1 | 54 | | talkingAnimator?.gameObject.SetActive(false); |
| 1 | 55 | | } |
| | 56 | |
|
| | 57 | | void OnEnable() |
| | 58 | | { |
| | 59 | | // We initialize mainCamera here because the main camera may change while the gameobject is disabled |
| 2 | 60 | | mainCamera = Camera.main; |
| 2 | 61 | | } |
| | 62 | |
|
| | 63 | | void LateUpdate() |
| | 64 | | { |
| 0 | 65 | | if (string.IsNullOrEmpty(nameText.text)) |
| 0 | 66 | | return; |
| | 67 | |
|
| 0 | 68 | | RefreshTextPosition(); |
| | 69 | |
|
| 0 | 70 | | if (Screen.width != res.x || Screen.height != res.y) |
| 0 | 71 | | nameText.SetAllDirty(); |
| | 72 | |
|
| 0 | 73 | | res = new Vector2(Screen.width, Screen.height); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private void RefreshTextPosition() |
| | 77 | | { |
| 0 | 78 | | Vector3 screenPoint = mainCamera == null ? Vector3.zero : mainCamera.WorldToViewportPoint(sourceTransform.positi |
| 0 | 79 | | uiContainer.alpha = 1.0f + (1.0f - (screenPoint.z / NAME_VANISHING_POINT_DISTANCE)); |
| | 80 | |
|
| 0 | 81 | | if (screenPoint.z > 0) |
| | 82 | | { |
| 0 | 83 | | if (!uiContainer.gameObject.activeSelf) |
| | 84 | | { |
| 0 | 85 | | uiContainer.gameObject.SetActive(true); |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | float width = canvasRect.rect.width; |
| 0 | 89 | | float height = canvasRect.rect.height; |
| 0 | 90 | | screenPoint.Scale(new Vector3(width, height, 0)); |
| 0 | 91 | | ((RectTransform)transform).anchoredPosition = screenPoint; |
| 0 | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 0 | 95 | | if (uiContainer.gameObject.activeSelf) |
| | 96 | | { |
| 0 | 97 | | uiContainer.gameObject.SetActive(false); |
| | 98 | | } |
| | 99 | | } |
| 0 | 100 | | } |
| | 101 | | } |