| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | namespace AvatarNamesHUD |
| | 6 | | { |
| | 7 | | public class AvatarNamesTracker |
| | 8 | | { |
| 1 | 9 | | private static readonly Vector3 OFFSET = new Vector3(0, 3f, 0); |
| 1 | 10 | | private static readonly int VOICE_CHAT_ANIMATOR_TALKING = Animator.StringToHash("Talking"); |
| | 11 | | private const float NAME_VANISHING_POINT_DISTANCE = 15.0f; |
| | 12 | |
|
| | 13 | | internal readonly RectTransform canvasRect; |
| | 14 | | internal readonly Image background; |
| | 15 | | internal readonly CanvasGroup backgroundCanvasGroup; |
| | 16 | | internal readonly TextMeshProUGUI name; |
| | 17 | | internal readonly CanvasGroup voiceChatCanvasGroup; |
| | 18 | | internal readonly Animator voiceChatAnimator; |
| | 19 | |
|
| | 20 | | internal Player player; |
| | 21 | | internal bool visibility = false; |
| | 22 | |
|
| 1 | 23 | | private static Camera mainCamera = null; |
| | 24 | |
|
| 212 | 25 | | public AvatarNamesTracker(RectTransform canvasRect, RectTransform backgroundRect, RectTransform nameRect, RectTr |
| | 26 | | { |
| 212 | 27 | | this.canvasRect = canvasRect; |
| 212 | 28 | | backgroundCanvasGroup = backgroundRect.GetComponent<CanvasGroup>(); |
| 212 | 29 | | background = backgroundRect.GetComponent<Image>(); |
| 212 | 30 | | name = nameRect.GetComponent<TextMeshProUGUI>(); |
| 212 | 31 | | voiceChatCanvasGroup = voiceChatRect.GetComponent<CanvasGroup>(); |
| 212 | 32 | | voiceChatAnimator = voiceChatRect.GetComponent<Animator>(); |
| 212 | 33 | | } |
| | 34 | |
|
| | 35 | | public void SetVisibility(bool visible) |
| | 36 | | { |
| 217 | 37 | | visibility = visible; |
| | 38 | |
|
| 217 | 39 | | if ( background != null ) |
| 217 | 40 | | background.gameObject.SetActive(visibility); |
| | 41 | |
|
| 217 | 42 | | if ( name != null ) |
| 217 | 43 | | name.gameObject.SetActive(visibility); |
| | 44 | |
|
| 217 | 45 | | voiceChatCanvasGroup.gameObject.SetActive(visibility && (player?.isTalking ?? false)); |
| 217 | 46 | | } |
| | 47 | |
|
| | 48 | | public void SetPlayer(Player newPlayer) |
| | 49 | | { |
| 5 | 50 | | player = newPlayer; |
| 5 | 51 | | if (player == null) |
| 1 | 52 | | return; |
| | 53 | |
|
| 4 | 54 | | name.text = newPlayer.name; |
| 4 | 55 | | name.ForceMeshUpdate(); //To get the new bounds |
| 4 | 56 | | UpdatePosition(); |
| 4 | 57 | | } |
| | 58 | |
|
| | 59 | | public void UpdatePosition() |
| | 60 | | { |
| 39 | 61 | | if (mainCamera == null) |
| 1 | 62 | | mainCamera = Camera.main; |
| | 63 | |
|
| 39 | 64 | | if (player == null || mainCamera == null) |
| 0 | 65 | | return; |
| | 66 | |
|
| 39 | 67 | | Vector3 screenPoint = mainCamera == null ? Vector3.zero : mainCamera.WorldToViewportPoint(player.worldPositi |
| 39 | 68 | | float alpha = screenPoint.z < 0 ? 0 : 1.0f + (1.0f - (screenPoint.z / NAME_VANISHING_POINT_DISTANCE)); |
| | 69 | |
|
| 39 | 70 | | if (screenPoint.z > 0) |
| | 71 | | { |
| 39 | 72 | | screenPoint.Scale(canvasRect.rect.size); |
| 39 | 73 | | name.rectTransform.anchoredPosition = screenPoint; |
| 39 | 74 | | background.rectTransform.anchoredPosition = screenPoint; |
| 39 | 75 | | background.rectTransform.sizeDelta = new Vector2(name.textBounds.extents.x * 2.5f, 30); |
| 39 | 76 | | Vector2 voiceChatOffset = -Vector2.Scale(Vector2.right, background.rectTransform.sizeDelta) * 0.5f; |
| 39 | 77 | | (voiceChatCanvasGroup.transform as RectTransform).anchoredPosition = background.rectTransform.anchoredPo |
| | 78 | |
|
| 39 | 79 | | voiceChatCanvasGroup.alpha = alpha; |
| 39 | 80 | | name.color = new Color(name.color.r, name.color.g, name.color.b, alpha); |
| 39 | 81 | | backgroundCanvasGroup.alpha = alpha; |
| 39 | 82 | | voiceChatCanvasGroup?.gameObject.SetActive(visibility && (player?.isTalking ?? false)); |
| 39 | 83 | | voiceChatAnimator.SetBool(VOICE_CHAT_ANIMATOR_TALKING, player.isTalking); |
| | 84 | |
|
| 39 | 85 | | background.gameObject.SetActive(visibility); |
| 39 | 86 | | name.gameObject.SetActive(visibility); |
| 39 | 87 | | voiceChatCanvasGroup.gameObject.SetActive(visibility && (player?.isTalking ?? false)); |
| 39 | 88 | | } |
| | 89 | | else |
| | 90 | | { |
| 0 | 91 | | background.gameObject.SetActive(false); |
| 0 | 92 | | name.gameObject.SetActive(false); |
| 0 | 93 | | voiceChatCanvasGroup.gameObject.SetActive(false); |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void DestroyUIElements() |
| | 98 | | { |
| 0 | 99 | | GameObject.Destroy(background.gameObject); |
| 0 | 100 | | Object.Destroy(name.gameObject); |
| 0 | 101 | | Object.Destroy(voiceChatCanvasGroup.gameObject); |
| 0 | 102 | | } |
| | 103 | | } |
| | 104 | | } |