From b19f1507253ed1838675343420404db333c6f89a Mon Sep 17 00:00:00 2001 From: Jason Durand Date: Wed, 1 Jun 2022 15:15:42 -0400 Subject: [PATCH] Added PlayerList --- Assets/Scripts/PlayerList.cs | 35 +++++++++++++++++++++++++++++++ Assets/Scripts/PlayerList.cs.meta | 11 ++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Assets/Scripts/PlayerList.cs create mode 100644 Assets/Scripts/PlayerList.cs.meta diff --git a/Assets/Scripts/PlayerList.cs b/Assets/Scripts/PlayerList.cs new file mode 100644 index 0000000..e2153d0 --- /dev/null +++ b/Assets/Scripts/PlayerList.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using TMPro; +using UnityEngine; + +public class PlayerList : MonoBehaviour { + HashSet playerIds = new(); + TMP_Text text; + + void Awake() { + text = GetComponent(); + text.text = ""; + } + + public void SetPlayerList(IEnumerable ids) { + playerIds.Clear(); + foreach (int id in ids) + playerIds.Add(id); + } + + public void Connected(int id) { + playerIds.Add(id); + UpdateList(); + } + + public void Disconnected(int id) { + playerIds.Remove(id); + UpdateList(); + } + + void UpdateList() { + text.text = ""; + foreach (int playerId in playerIds) + text.text += $"Player #{playerId}\n"; + } +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerList.cs.meta b/Assets/Scripts/PlayerList.cs.meta new file mode 100644 index 0000000..4cf6775 --- /dev/null +++ b/Assets/Scripts/PlayerList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8f4454c6a8ff1f5478829232f04817a5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: