mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
fix: Decouple ChatWindow from player (#1429)
This commit is contained in:
parent
bc40d871b8
commit
42a2f9b853
@ -47,6 +47,8 @@ private void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage cr
|
||||
|
||||
// set it as the player
|
||||
NetworkServer.AddPlayerForConnection(connection, playergo);
|
||||
|
||||
chatWindow.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,23 @@ public class ChatWindow : MonoBehaviour
|
||||
public Text chatHistory;
|
||||
public Scrollbar scrollbar;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
|
||||
Player.OnMessage += OnPlayerMessage;
|
||||
|
||||
}
|
||||
|
||||
private void OnPlayerMessage(Player player, string message)
|
||||
{
|
||||
string prettyMessage = player.isLocalPlayer ?
|
||||
$"<color=red>{player.playerName}: </color> {message}" :
|
||||
$"<color=blue>{player.playerName}: </color> {message}";
|
||||
AppendMessage(prettyMessage);
|
||||
|
||||
Debug.Log(message);
|
||||
}
|
||||
|
||||
public void OnSend()
|
||||
{
|
||||
if (chatMessage.text.Trim() == "") return;
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@ -9,7 +10,7 @@ public class Player : NetworkBehaviour
|
||||
[SyncVar]
|
||||
public string playerName;
|
||||
|
||||
public ChatWindow chatWindow => ((ChatNetworkManager)NetworkManager.singleton).chatWindow;
|
||||
public static event Action<Player, string> OnMessage;
|
||||
|
||||
[Command]
|
||||
public void CmdSend(string message)
|
||||
@ -18,21 +19,10 @@ public void CmdSend(string message)
|
||||
RpcReceive(message.Trim());
|
||||
}
|
||||
|
||||
public override void OnStartLocalPlayer()
|
||||
{
|
||||
chatWindow.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
public void RpcReceive(string message)
|
||||
{
|
||||
string prettyMessage = isLocalPlayer ?
|
||||
$"<color=red>{playerName}: </color> {message}" :
|
||||
$"<color=blue>{playerName}: </color> {message}";
|
||||
|
||||
chatWindow.AppendMessage(prettyMessage);
|
||||
|
||||
Debug.Log(message);
|
||||
OnMessage?.Invoke(this, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user