using TMPro;
using UnityEngine;
///
/// 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 .
///
[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(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);
}
}
}