Pun-01/Assets/Scripts/PlayerNameInputField.cs
2022-07-08 09:14:55 +08:00

42 lines
1.1 KiB
C#

using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace cc.ivanli.pun01
{
[RequireComponent(typeof(TMP_InputField))]
public class PlayerNameInputField : MonoBehaviour
{
private const string playerNamePrefKey = "PlayerName";
public void Start()
{
string defaultName = string.Empty;
TMP_InputField inputField = GetComponent<TMP_InputField>();
if (inputField != null)
{
if (PlayerPrefs.HasKey(playerNamePrefKey))
{
defaultName = PlayerPrefs.GetString(playerNamePrefKey);
inputField.text = defaultName;
}
}
PhotonNetwork.NickName = defaultName;
}
public void SetPlayerName(string value)
{
if (string.IsNullOrEmpty(value)) {
Debug.LogError("Player Name is not null or empty");
return;
}
PhotonNetwork.NickName = value;
PlayerPrefs.SetString(playerNamePrefKey, value);
}
}
}