This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/PlayerNameInputField.cs

46 lines
No EOL
1.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace Com.MyCompany.MyGame
{
/// <summary>
/// Player name input field. Let the user input his name, will appear above the player in the game.
/// </summary>
[RequireComponent(typeof(InputField))]
public class PlayerNameInputField : MonoBehaviour
{
// Store the PlayerPref Key to avoid typos
static string playerNamePrefKey = "PlayerName";
void Start()
{
string defaultName = "";
InputField _inputField = this.GetComponent<InputField>();
if (_inputField != null)
{
if (PlayerPrefs.HasKey(playerNamePrefKey))
{
defaultName = PlayerPrefs.GetString(playerNamePrefKey);
_inputField.text = defaultName;
}
}
PhotonNetwork.playerName = defaultName;
}
/// <summary>
/// Sets the name of the player, and save it in the PlayerPrefs for future sessions.
/// </summary>
/// <param name="value">The name of the Player</param>
public void SetPlayerName(string value)
{
// force a trailing space string in case value is an empty string, else playerName would not be updated.
PhotonNetwork.playerName = value + " ";
PlayerPrefs.SetString(playerNamePrefKey, value);
}
}
}