Assign and sync player name and color on connect
This commit is contained in:
parent
c4528663ce
commit
b03afdea64
|
@ -115,6 +115,7 @@ GameObject:
|
||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 6498975337014063786}
|
- component: {fileID: 6498975337014063786}
|
||||||
- component: {fileID: 2811985185363063978}
|
- component: {fileID: 2811985185363063978}
|
||||||
|
- component: {fileID: 1756213952929103018}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Player
|
m_Name: Player
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
|
@ -155,3 +156,18 @@ MonoBehaviour:
|
||||||
serverOnly: 0
|
serverOnly: 0
|
||||||
visible: 0
|
visible: 0
|
||||||
hasSpawned: 0
|
hasSpawned: 0
|
||||||
|
--- !u!114 &1756213952929103018
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 9197140212603367960}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 91a604620298dff4fb1301fafef543f7, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
syncDirection: 0
|
||||||
|
syncMode: 0
|
||||||
|
syncInterval: 0
|
||||||
|
|
|
@ -3,15 +3,12 @@ using UnityEngine;
|
||||||
|
|
||||||
public class MyNetworkManager : NetworkManager
|
public class MyNetworkManager : NetworkManager
|
||||||
{
|
{
|
||||||
public override void OnClientConnect()
|
|
||||||
{
|
|
||||||
base.OnClientConnect();
|
|
||||||
Debug.LogError("Client connected to the server!");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
|
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
|
||||||
{
|
{
|
||||||
base.OnServerAddPlayer(conn);
|
base.OnServerAddPlayer(conn);
|
||||||
Debug.LogError($"Player connected! total connected players: {numPlayers.ToString()}");
|
|
||||||
|
MyNetworkPlayer player = conn.identity.GetComponent<MyNetworkPlayer>();
|
||||||
|
player.SetDisplayName($"Player {numPlayers.ToString()}");
|
||||||
|
player.SetPlayerColor(new(Random.value, Random.value , Random.value ));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
using Mirror;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class MyNetworkPlayer : NetworkBehaviour
|
||||||
|
{
|
||||||
|
[SyncVar] [SerializeField] private string _displayName = "Missing Name";
|
||||||
|
[SyncVar] [SerializeField] private Color _playerColor = Color.black;
|
||||||
|
|
||||||
|
public void SetDisplayName(string newDisplayName)
|
||||||
|
{
|
||||||
|
_displayName = newDisplayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetPlayerColor(Color newPlayerColor)
|
||||||
|
{
|
||||||
|
_playerColor = newPlayerColor;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 91a604620298dff4fb1301fafef543f7
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue