我正在做一个多人游戏,我想知道是否有一个场景,当玩家按下继续按钮将其保存时,他会创建他的名字(输入字段和继续按钮)?我使用了此输入字段脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Photon.Pun;
using UnityEngine.UI;
public class PlayerNameInput : MonoBehaviour
{
[SerializeField] private TMP_InputField nameInputField = null;
[SerializeField] private Button continueButton = null;
private const string PlayerPrefsNameKey = "PlayerName";
private void Start() => SetUpInputField();
private void SetUpInputField()
{
if (!PlayerPrefs.HasKey(PlayerPrefsNameKey)) { return; }
string defaultName = PlayerPrefs.GetString(PlayerPrefsNameKey);
nameInputField.text = defaultName;
SetPlayerName(defaultName);
}
public void SetPlayerName(string name)
{
continueButton.interactable = !string.IsNullOrEmpty(name);
}
public void SavePlayerName()
{
string PlayerName = nameInputField.text;
PhotonNetwork.NickName = PlayerName;
PlayerPrefs.SetString(PlayerPrefsNameKey, PlayerName);
}
}
,在继续按钮上,我使用Find Opponent();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
public class Incercare : MonoBehaviourPunCallbacks
{
[SerializeField] private GameObject findOpponentPanel = null;
[SerializeField] private GameObject waitingPanel = null;
[SerializeField] private Text waitingstatusText = null;
private bool isConnecting = false;
private const string GameVersion = "0.1";
private const int MaxPlayersPerRoom = 3;
private void Awake()
{
PhotonNetwork.AutomaticallySyncScene = true;
}
public void FindOpponent()
{
PhotonNetwork.LoadLevel("HomeMenu");
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinRandomRoom();
}
public override void OnDisconnected(DisconnectCause cause)
{
waitingPanel.SetActive(false);
findOpponentPanel.SetActive(true);
Debug.Log($"Diconnected due to {cause}");
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
Debug.Log("No clients are waiting for an opponent,creating a new room");
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = MaxPlayersPerRoom });
}
public override void OnJoinedRoom()
{
Debug.Log("Client Successfully joined a room");
int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
if (playerCount != 2)
{
waitingstatusText.text = " Waiting for Opponent";
Debug.Log("Client is waiting for an opponent");
}
else
{
waitingstatusText.text = "Opponent Found";
Debug.Log("Matching is ready to begin");
}
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
if (PhotonNetwork.CurrentRoom.PlayerCount == MaxPlayersPerRoom)
{
PhotonNetwork.CurrentRoom.IsOpen = false;
Debug.Log("Match is ready to begin");
waitingstatusText.text = "Opponent found";
PhotonNetwork.LoadLevel("RoomPlay");
}
}
}
但是我不确定是否在更改场景的过程中保存了用户名,因为下一个场景是其播放器具有以下3个选项的HomeMenu:创建房间,选择房间或随机开始