using UnityEngine; using UnityEngine.SceneManagement; using TMPro; using System.Collections; using System.Text.RegularExpressions; // Add this for regex functionality public class UserNameInput : MonoBehaviour { [SerializeField] private TMP_InputField inputField; [SerializeField] private TextMeshProUGUI greetingText; // Add reference to the greeting text public void SavePlayerName() { // Get the inputted name string playerName = inputField.text; // Validate the username if (IsValidUserName(playerName)) { // Display the greeting greetingText.text = "Welcome to ArcyRevenge " + playerName; // Save the player's name using PlayerPrefs PlayerPrefs.SetString("PlayerName", playerName); // Optionally, delay before loading the scene to show the greeting StartCoroutine(LoadSceneAfterDelay(2f)); // 2-second delay } else { // Display error message if the username is invalid greetingText.text = "Invalid username, try again."; } } private bool IsValidUserName(string userName) { // Check if the username length is greater than 10 characters if (userName.Length > 10) { return false; } // Check if the username contains any numbers if (Regex.IsMatch(userName, @"\d")) { return false; } // Username is valid return true; } private IEnumerator LoadSceneAfterDelay(float delay) { yield return new WaitForSeconds(delay); // Load the first scene SceneManager.LoadScene("Scene1"); } }