Files
chameleon/Assets/Scripts/Network/UI/PlayerNameField.cs
T
adminandClaude Fable 5 0e7ce0c410 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>
2026-07-08 14:58:15 +02:00

45 lines
1.3 KiB
C#

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);
}
}
}