fix: Updated Chat example

This commit is contained in:
MrGadget1024 2021-12-01 08:01:11 -05:00
parent 65f6c09599
commit be23d3a67a
4 changed files with 61 additions and 27 deletions

View File

@ -1313,7 +1313,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1783103025}
m_MethodName: set_PlayerName
m_MethodName: SetPlayername
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
@ -2199,7 +2199,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 75861000}
m_MethodName: OnSend
m_MethodName: SendMessage
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
@ -3022,6 +3022,8 @@ MonoBehaviour:
SendWindowSize: 4096
ReceiveWindowSize: 4096
NonAlloc: 1
ReliableMaxMessageSize: 298449
UnreliableMaxMessageSize: 1199
debugLog: 0
statisticsGUI: 0
statisticsLog: 0
@ -3038,7 +3040,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
dontDestroyOnLoad: 1
PersistNetworkManagerToOfflineScene: 0
runInBackground: 1
autoStartServerBuild: 1
serverTickRate: 30
@ -3047,8 +3048,6 @@ MonoBehaviour:
transport: {fileID: 1783103023}
networkAddress: localhost
maxConnections: 100
disconnectInactiveConnections: 0
disconnectInactiveTimeout: 60
authenticator: {fileID: 0}
playerPrefab: {fileID: 5075528875289742095, guid: e5905ffa27de84009b346b49d518ba03,
type: 3}
@ -3056,6 +3055,7 @@ MonoBehaviour:
playerSpawnMethod: 0
spawnPrefabs: []
chatWindow: {fileID: 75861000}
PlayerName:
--- !u!4 &1783103026
Transform:
m_ObjectHideFlags: 0

View File

@ -1,5 +1,10 @@
using UnityEngine;
/*
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
*/
namespace Mirror.Examples.Chat
{
[AddComponentMenu("")]
@ -8,34 +13,25 @@ public class ChatNetworkManager : NetworkManager
[Header("Chat GUI")]
public ChatWindow chatWindow;
// Set by UI element UsernameInput OnValueChanged
public string PlayerName { get; set; }
public string PlayerName;
// Called by UI element NetworkAddressInput.OnValueChanged
public void SetHostname(string hostname)
{
networkAddress = hostname;
}
#region Messages
public struct CreatePlayerMessage : NetworkMessage
{
public string name;
}
#endregion
#region Server
public override void OnStartServer()
{
base.OnStartServer();
NetworkServer.RegisterHandler<CreatePlayerMessage>(OnCreatePlayer);
}
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
// tell the server to create a player with this name
conn.Send(new CreatePlayerMessage { name = PlayerName });
}
void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage createPlayerMessage)
{
// create a gameobject using the name supplied by client
@ -47,5 +43,31 @@ void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage createPlay
chatWindow.gameObject.SetActive(true);
}
#endregion
#region Client
// Called by UI element UsernameInput.OnValueChanged
public void SetPlayername(string playerName)
{
PlayerName = playerName;
}
// Called by UI element NetworkAddressInput.OnValueChanged
public void SetHostname(string hostname)
{
networkAddress = hostname;
}
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
// tell the server to create a player with this name
conn.Send(new CreatePlayerMessage { name = PlayerName });
}
#endregion
}
}

View File

@ -13,22 +13,36 @@ public class ChatWindow : MonoBehaviour
public void Awake()
{
Player.OnMessage += OnPlayerMessage;
chatMessage.onEndEdit.AddListener(OnEndEdit);
}
void OnEndEdit(string input)
{
if (Input.GetKeyDown(KeyCode.Return)
|| Input.GetKeyDown(KeyCode.KeypadEnter)
|| Input.GetButtonDown("Submit"))
{
//Debug.Log($"OnEndEdit {input}");
SendMessage();
chatMessage.text = string.Empty;
chatMessage.ActivateInputField();
}
}
void OnPlayerMessage(Player player, string message)
{
string prettyMessage = player.isLocalPlayer ?
$"<color=red>{player.playerName}: </color> {message}" :
$"<color=blue>{player.playerName}: </color> {message}";
$"<color=red>{player.playerName}:</color> {message}" :
$"<color=blue>{player.playerName}:</color> {message}";
AppendMessage(prettyMessage);
Debug.Log(message);
}
// Called by UI element SendButton.OnClick
public void OnSend()
public void SendMessage()
{
if (chatMessage.text.Trim() == "")
if (chatMessage.text.Trim() == string.Empty)
return;
// get our player
@ -36,8 +50,6 @@ public void OnSend()
// send a message
player.CmdSend(chatMessage.text.Trim());
chatMessage.text = "";
}
internal void AppendMessage(string message)

View File

@ -12,7 +12,7 @@ public class Player : NetworkBehaviour
[Command]
public void CmdSend(string message)
{
if (message.Trim() != "")
if (message.Trim() != string.Empty)
RpcReceive(message.Trim());
}