fix: Decouple ChatWindow from player (#1429)

This commit is contained in:
Paul Pacheco 2020-01-12 16:53:14 -06:00 committed by GitHub
parent bc40d871b8
commit 42a2f9b853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 13 deletions

View File

@ -47,6 +47,8 @@ private void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage cr
// set it as the player // set it as the player
NetworkServer.AddPlayerForConnection(connection, playergo); NetworkServer.AddPlayerForConnection(connection, playergo);
chatWindow.gameObject.SetActive(true);
} }
} }
} }

View File

@ -12,6 +12,23 @@ public class ChatWindow : MonoBehaviour
public Text chatHistory; public Text chatHistory;
public Scrollbar scrollbar; 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() public void OnSend()
{ {
if (chatMessage.text.Trim() == "") return; if (chatMessage.text.Trim() == "") return;

View File

@ -1,3 +1,4 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -9,7 +10,7 @@ public class Player : NetworkBehaviour
[SyncVar] [SyncVar]
public string playerName; public string playerName;
public ChatWindow chatWindow => ((ChatNetworkManager)NetworkManager.singleton).chatWindow; public static event Action<Player, string> OnMessage;
[Command] [Command]
public void CmdSend(string message) public void CmdSend(string message)
@ -18,21 +19,10 @@ public void CmdSend(string message)
RpcReceive(message.Trim()); RpcReceive(message.Trim());
} }
public override void OnStartLocalPlayer()
{
chatWindow.gameObject.SetActive(true);
}
[ClientRpc] [ClientRpc]
public void RpcReceive(string message) public void RpcReceive(string message)
{ {
string prettyMessage = isLocalPlayer ? OnMessage?.Invoke(this, message);
$"<color=red>{playerName}: </color> {message}" :
$"<color=blue>{playerName}: </color> {message}";
chatWindow.AppendMessage(prettyMessage);
Debug.Log(message);
} }
} }
} }