Add player names: settings persistence, replication, screen-space nameplates
- GameSettings: playerName field persisted in Settings.json - NetworkPlayerSetup: owner-written DisplayName NetworkVariable (seeded from settings on spawn, sanitized/truncated, live rename push) - PlayerNameField: binds the session panel name input to settings - PlayerNameplates: screen-space overlay labels above players with distance-based font scaling (clamped min/max), pooling, FP self-hide Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6db78397da
commit
0e7ce0c410
6 files changed
+252
No files matched your search
@@ -0,0 +1,44 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Binds the session panel's player-name input to the persisted name (GameSettings/Settings.json).
|
||||
/// Seeds the field on enable, saves on end-edit, and pushes a live rename to the already-spawned local
|
||||
/// avatar (the Esc overlay keeps the panel reachable while connected); fresh spawns read GameSettings
|
||||
/// themselves in <see cref="NetworkPlayerSetup.OnNetworkSpawn"/>.
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class PlayerNameField : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Name input. Empty = the first TMP_InputField below this object.")]
|
||||
[SerializeField] private TMP_InputField input;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (!input) input = GetComponentInChildren<TMP_InputField>(true);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (!input) return;
|
||||
input.SetTextWithoutNotify(GameSettings.PlayerName);
|
||||
input.onEndEdit.AddListener(OnEndEdit);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (input) input.onEndEdit.RemoveListener(OnEndEdit);
|
||||
}
|
||||
|
||||
private void OnEndEdit(string value)
|
||||
{
|
||||
GameSettings.PlayerName = value;
|
||||
|
||||
var avatars = NetworkPlayerSetup.Active;
|
||||
for (int i = 0; i < avatars.Count; i++)
|
||||
{
|
||||
var avatar = avatars[i];
|
||||
if (avatar && avatar.IsOwner) avatar.PublishDisplayName(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user