// --------------------------------------------------------------------------------------------------------------------
//
// Part of: Photon Unity Networking Demos
//
//
// Let the player input his name to be saved as the network player Name, viewed by alls players above each when in the same room.
//
// developer@exitgames.com
// --------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using UnityEngine.UI;
namespace Photon.Pun.Demo.PunBasics
{
///
/// Player name input field. Let the user input his name, will appear above the player in the game.
///
[RequireComponent(typeof(InputField))]
public class PlayerNameInputField : MonoBehaviour
{
#region Private Constants
// Store the PlayerPref Key to avoid typos
const string playerNamePrefKey = "PlayerName";
#endregion
#region MonoBehaviour CallBacks
///
/// MonoBehaviour method called on GameObject by Unity during initialization phase.
///
void Start () {
string defaultName = string.Empty;
InputField _inputField = this.GetComponent();
if (_inputField!=null)
{
if (PlayerPrefs.HasKey(playerNamePrefKey))
{
defaultName = PlayerPrefs.GetString(playerNamePrefKey);
_inputField.text = defaultName;
}
}
PhotonNetwork.NickName = defaultName;
}
#endregion
#region Public Methods
///
/// Sets the name of the player, and save it in the PlayerPrefs for future sessions.
///
/// The name of the Player
public void SetPlayerName(string value)
{
// #Important
if (string.IsNullOrEmpty(value))
{
Debug.LogError("Player Name is null or empty");
return;
}
PhotonNetwork.NickName = value;
PlayerPrefs.SetString(playerNamePrefKey, value);
}
#endregion
}
}