Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion For those who wants any chars in their player name

Discussion in 'Authentication' started by EnamulIslamJisan, Aug 17, 2023.

  1. EnamulIslamJisan


    Oct 11, 2018
    I was trying to let users have whitespace in their name which currently is not supported in the auth package and I did not know how to do it I think there are lots of you who faced the same (probably). I have found an excellent way to add any characters you want in the player's name. It will also let you know if the name was unchanged or not at the beginning of the game. Here are the steps:

    Get Player Name:
    • Check for custom character prefixes to validate if the name was changed or not. e.g. (--PlayerName#0000) where the #0000 is auto-added by unity and the -- is your custom prefix which won't be available by unity's auto-generated name so you'll know it was unchanged if the prefix is missing
    • After the prefix should be the name of the player and it should be a 64-bit string to get the real name after the raw name is fetched just convert the 64-bit value to a normal string.
    Set Player Name:
    • Do your custom validation on the user's input
    • Convert the string to 64bit and add -- prefix
    • Update the name
    Below is a code example I did to let players have any of UTF* characters or whitespace in their player names. No problem if this is not important to you but might be important to someone on the globe. Until Unity removes the limitation this is one workaround to let users have custom names.

    Code (CSharp):
    1. public async Task GetPlayerNameAsync()
    2.     {
    3.         if (!AuthenticationService.Instance.IsSignedIn) return;
    4.         await AuthenticationService.Instance.GetPlayerNameAsync();
    5.         var rawPlayerName = AuthenticationService.Instance.PlayerName;
    6.         if(rawPlayerName.StartsWith("--"))
    7.         {
    8.             rawPlayerName = rawPlayerName.Replace("--", "");
    9.             var rawPlayerNameParts = rawPlayerName.Split('#');
    10.             PlayerName = Encoding.UTF8.GetString(Convert.FromBase64String(rawPlayerNameParts[0]));
    11.             PlayerTag = rawPlayerNameParts[1];
    12.         }
    13.         else
    14.         {
    15.             //Player name was autoGenerated and was never changed yet
    16.         }
    17.     }
    19.     public async Task SetPlayerNameAsync(string name)
    20.     {
    21.         if (!AuthenticationService.Instance.IsSignedIn) return;
    22.         if (string.IsNullOrEmpty(name))
    23.         {
    24.             Debug.LogError("New name cannot be empty");
    25.             return;
    26.         }
    27.         name = $"--{Convert.ToBase64String(Encoding.UTF8.GetBytes(name))}";
    28.         if (name == PlayerName)
    29.         {
    30.             Debug.LogError("New name cannot be same as current name");
    31.             return;
    32.         }
    34.         await AuthenticationService.Instance.UpdatePlayerNameAsync(name);
    35.         await GetPlayerNameAsync();
    36.         Debug.Log("Name changed successfully!");
    37.     }
    Last edited: Aug 24, 2023