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
|
// set it as the player
|
||||||
NetworkServer.AddPlayerForConnection(connection, playergo);
|
NetworkServer.AddPlayerForConnection(connection, playergo);
|
||||||
|
|
||||||
|
chatWindow.gameObject.SetActive(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user