-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathServerHostClientText.cs
More file actions
79 lines (69 loc) · 1.73 KB
/
ServerHostClientText.cs
File metadata and controls
79 lines (69 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
public class ServerHostClientText : NetworkBehaviour
{
private Text m_DisplayText;
private Color m_Color;
private Color m_ColorAlpha;
private Vector3 m_LocalPosition;
public void SetColor(Color color)
{
m_Color = color;
m_ColorAlpha = color;
m_ColorAlpha.a = 0.35f;
}
private void Awake()
{
m_LocalPosition = transform.localPosition;
m_DisplayText = GetComponent<Text>();
}
private void Start()
{
if (m_DisplayText != null)
{
m_DisplayText.text = string.Empty;
SetColor(m_DisplayText.color);
}
}
public override void OnNetworkSpawn()
{
if (m_DisplayText != null)
{
if (NetworkManager.IsServer)
{
m_DisplayText.text = NetworkManager.IsHost ? "Host" : "Server";
}
else if (NetworkManager.IsClient)
{
m_DisplayText.text = $"Client-{NetworkManager.LocalClientId}";
}
}
transform.localPosition = m_LocalPosition;
}
public override void OnNetworkDespawn()
{
if (m_DisplayText != null)
{
m_DisplayText.text = string.Empty;
}
base.OnNetworkDespawn();
}
private bool m_LastFocusedValue;
private void OnGUI()
{
if (!IsSpawned || m_LastFocusedValue == Application.isFocused)
{
return;
}
m_LastFocusedValue = Application.isFocused;
if (m_LastFocusedValue)
{
m_DisplayText.color = m_Color;
}
else
{
m_DisplayText.color = m_ColorAlpha;
}
}
}