Setting name and color on client is handled by server and updated to clients

This commit is contained in:
Stedd 2023-09-02 15:03:47 +02:00
parent a34545ae26
commit 3324cf1f98
5 changed files with 65 additions and 17 deletions

View File

@ -0,0 +1,6 @@
using UnityEngine;
public static class ColorManipulation
{
public static Color GetRandomColor() => new(Random.value, Random.value, Random.value);
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 200a2e870f62694458ff9e42c9fa7ff5 guid: 5c5b7d3af1f171c46b09250081a08927
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -9,8 +9,8 @@ public class MyNetworkManager : NetworkManager
MyNetworkPlayer player = conn.identity.GetComponent<MyNetworkPlayer>(); MyNetworkPlayer player = conn.identity.GetComponent<MyNetworkPlayer>();
player.SetDisplayName($"Player {numPlayers.ToString()}"); player.SetDisplayName($"Player {numPlayers.ToString()}");
player.SetPlayerColor(GetRandomColor()); player.SetPlayerColor(ColorManipulation.GetRandomColor());
} }
private static Color GetRandomColor() => new(Random.value, Random.value, Random.value);
} }

View File

@ -1,4 +1,3 @@
using System;
using Mirror; using Mirror;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@ -7,6 +6,7 @@ public class MyNetworkPlayer : NetworkBehaviour
{ {
[Header("Connections")] [Header("Connections")]
[SerializeField] private TMP_Text _playerNameObject; [SerializeField] private TMP_Text _playerNameObject;
[Header("Variables")] [Header("Variables")]
[SyncVar(hook = nameof(SetPlayerNameTagText))] [SyncVar(hook = nameof(SetPlayerNameTagText))]
[SerializeField] [SerializeField]
@ -15,7 +15,9 @@ public class MyNetworkPlayer : NetworkBehaviour
[SyncVar(hook = nameof(SetPlayerNameTagColor))] [SyncVar(hook = nameof(SetPlayerNameTagColor))]
[SerializeField] [SerializeField]
private Color _playerColor = Color.black; private Color _playerColor = Color.black;
#region Server
[Server] [Server]
public void SetDisplayName(string newDisplayName) public void SetDisplayName(string newDisplayName)
{ {
@ -28,6 +30,43 @@ public class MyNetworkPlayer : NetworkBehaviour
_playerColor = newPlayerColor; _playerColor = newPlayerColor;
} }
[Command]
private void CmdSetDisplayName(string newDisplayName)
{
if (NameLengthIsCorrect(newDisplayName))
{
RpcDebugLog($"Setting player names{newDisplayName}");
SetDisplayName(newDisplayName);
}
else
{
RpcDebugLog($"new name: {newDisplayName} : is too long");
}
}
[Command]
private void CmdSetNewColor()
{
Color newColor = ColorManipulation.GetRandomColor();
RpcDebugLog($"Setting player color{newColor.ToString()}");
SetPlayerColor(newColor);
}
private bool NameLengthIsCorrect(string newDisplayName)
{
return newDisplayName.Length < 10;
}
[ClientRpc]
private void RpcDebugLog(string message)
{
Debug.Log(message);
}
#endregion
#region Client
private void SetPlayerNameTagText(string oldText, string newText) private void SetPlayerNameTagText(string oldText, string newText)
{ {
if (_playerNameObject != null) if (_playerNameObject != null)
@ -43,4 +82,18 @@ public class MyNetworkPlayer : NetworkBehaviour
_playerNameObject.color = newColor; _playerNameObject.color = newColor;
} }
} }
[ContextMenu("SetName")]
private void SetMyName()
{
CmdSetDisplayName("New Name");
}
[ContextMenu("NewColor")]
private void SetNewColor()
{
CmdSetNewColor();
}
#endregion
} }

View File

@ -1,11 +0,0 @@
using Mirror;
using UnityEngine;
public class MyNetworkPlayer1 : NetworkBehaviour
{
[SyncVar]
[SerializeField]
private Transform _transform;
}