Skip to content

Commit

Permalink
Nickname
Browse files Browse the repository at this point in the history
Nickname is empty by default in editor mode  hence for testing purposes  this changeset enable the nickname to be set from editor.
  • Loading branch information
sbanca committed Jan 20, 2025
1 parent 91f3ab4 commit 24ac381
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
25 changes: 25 additions & 0 deletions Assets/Editor/MultiplayerManagerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class MultiplayerManagerInspector : Editor
{
private MultiplayerManager multiplayerManager;
private string roomName = "1234";
private string nickname = "PlayerNickname";
private string oldNickname = "PlayerNickname";
private bool isPrivate = false;
private int maxPlayers = 4;
private bool voiceDisabled = false;
Expand All @@ -26,6 +28,14 @@ public override void OnInspectorGUI()
GUILayout.Space(10);

roomName = EditorGUILayout.TextField("Room Name", roomName);
nickname = EditorGUILayout.TextField("Nickname", nickname);

if (nickname != oldNickname)
{
SetNickname();
oldNickname = nickname;
EditorUtility.SetDirty(target);
}

//State
string connectionState = "";
Expand Down Expand Up @@ -160,6 +170,21 @@ private async void ConnectToRoom()
}
}

private async void SetNickname()
{
if (multiplayerManager != null)
{

ConnectionUserInfo ui = new ConnectionUserInfo
{
Nickname = nickname,
UserId = MultiplayerManager.m_Instance.UserInfo.UserId,
Role = MultiplayerManager.m_Instance.UserInfo.Role
};
MultiplayerManager.m_Instance.UserInfo = ui;
}
}

private async void DisconnectFromRoom()
{
if (multiplayerManager != null)
Expand Down
16 changes: 9 additions & 7 deletions Assets/Scripts/GUI/MultiplayerRoomOptionsPopUpWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MultiplayerRoomOptionsPopUpWindow : PopUpWindow
public Vector2 PlayerGuiPrefabSize;
public Vector2 PlayerListOffset;
public Vector2 PlayerListArea;
private List<GameObject> m_instantiatedGuiPrefabs;
private List<GameObject> m_instantiatedGuiPrefabs = new List<GameObject>();

public RemotePlayers m_RemotePlayers
{
Expand Down Expand Up @@ -70,6 +70,7 @@ override protected void BaseUpdate()
protected override void UpdateOpening()
{
base.UpdateOpening();
GeneratePlayerList();
}

protected override void UpdateClosing()
Expand Down Expand Up @@ -111,20 +112,21 @@ public void GeneratePlayerList(List<RemotePlayer> playersList = null)

List<RemotePlayer> playersToDisplay = playersList ?? m_RemotePlayers.List;

Vector3 basePosition = transform.position + new Vector3(-PlayerListArea.x / 2 + PlayerGuiPrefabSize.x / 2, PlayerListOffset.y + PlayerListArea.y / 2 - PlayerGuiPrefabSize.y / 2, 0);
Vector3 basePosition = new Vector3(
-PlayerListArea.x / 2 + PlayerGuiPrefabSize.x / 2,
PlayerListOffset.y + PlayerListArea.y / 2 - PlayerGuiPrefabSize.y / 2,
0
);
float yOffset = PlayerGuiPrefabSize.y;

foreach (var remotePlayer in playersToDisplay)
{
GameObject playerListItem = Instantiate(m_playerGuiPrefab, basePosition, Quaternion.identity, transform);
GameObject playerListItem = Instantiate(m_playerGuiPrefab, basePosition, transform.rotation, transform);
playerListItem.name = $"Player_{remotePlayer.PlayerId}";
playerListItem.transform.localPosition = basePosition;

PlayerListItemPrefab playerGuiComponent = playerListItem.GetComponent<PlayerListItemPrefab>();
if (playerGuiComponent != null)
{
playerGuiComponent.SetRemotePlayer(remotePlayer);
}
if (playerGuiComponent != null) playerGuiComponent.SetRemotePlayer(remotePlayer);

basePosition -= new Vector3(0, yOffset, 0);
playerListItem.SetActive(true);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Multiplayer/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MultiplayerManager : MonoBehaviour
private IVoiceConnectionHandler m_VoiceManager;

public ITransientData<PlayerRigData> m_LocalPlayer;
public RemotePlayers m_RemotePlayers;
[HideInInspector] public RemotePlayers m_RemotePlayers;

public Action<int, ITransientData<PlayerRigData>> localPlayerJoined;
public Action<RemotePlayer> remotePlayerJoined;
Expand Down
5 changes: 2 additions & 3 deletions Assets/Scripts/Multiplayer/Photon/PhotonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public void Update()
}
}


#region IConnectionHandler Methods

public async Task<bool> Connect()
Expand Down Expand Up @@ -287,8 +286,8 @@ public string GetPlayerNickname(int playerId)
.FirstOrDefault(playerRig => playerRig != null && playerRig.PlayerId == playerId);

if (remotePlayer != null && remotePlayer.Object != null && remotePlayer.Object.IsValid)
return remotePlayer.Nickname;
else return "default";
return remotePlayer.PersistentNickname;
else return "";
}

public GameObject GetPlayerPrefab(int playerId)
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/Multiplayer/Photon/PhotonPlayerRig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class PhotonPlayerRig : NetworkBehaviour, ITransientData<PlayerRigData>
[Networked] public float SceneScale { get; set; }
[Networked] public bool isReceivingVoiceTransmission { get; set; }
[Networked] public string Nickname { get; set; }

public string PersistentNickname = "";


PointerScript transientPointer;
// The offset transforms.
Expand Down Expand Up @@ -130,6 +133,7 @@ public PlayerRigData ReceiveData()
{
data.Nickname = this.Nickname;
NicknameText.text = this.Nickname;
PersistentNickname = this.Nickname;
}

}
Expand Down
23 changes: 22 additions & 1 deletion Assets/Scripts/Multiplayer/RemotePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,35 @@ namespace OpenBrush.Multiplayer
public class RemotePlayer
{
public int PlayerId;
public string Nickname;
private string _nickname;
public string Nickname
{

get
{
if (_nickname == "") _nickname = RetrieveNickNameFromRig();
return _nickname;
}
set { _nickname = value; }

}

// send/receive rig data interface
public ITransientData<PlayerRigData> TransientData;

// The underlying GameObjects in the scene that represents this player
public GameObject PlayerGameObject;
public GameObject VoiceGameObject;

private string RetrieveNickNameFromRig()
{

if (PlayerGameObject == null) return "";
PhotonPlayerRig Rig = PlayerGameObject.GetComponent<PhotonPlayerRig>();
if (Rig == null) return "";
return Rig.PersistentNickname;

}
}


Expand Down

0 comments on commit 24ac381

Please sign in to comment.