Unity Multiplayer Chat
Toxic behavior kills player retention. With our Unity SDK, you can sanitize in-game chat directly from the client (or authorized server), ensuring a safe environment for your player base without building complex word filters.
01. IMPORT PACKAGE
Add the SafeComms SDK via Unity Package Manager:
// In Unity Package Manager:
// Add package from git URL:
https://github.com/safecomms/react-unity-sdk.git02. CHAT SCRIPT
Attach this script to your Chat UI GameObject:
using UnityEngine;
using SafeComms.Unity; // From our Unity Package
using TMPro;
public class ChatController : MonoBehaviour
{
[Header("UI References")]
public TMP_InputField chatInput;
public TextMeshProUGUI chatHistory;
// > INITIALIZE_CLIENT
private SafeCommsClient _client;
void Start()
{
// SafeCommsClient is a singleton configured in your Resources or Inspector
_client = SafeCommsClient.Instance;
}
public async void OnSubmitChat()
{
string message = chatInput.text;
if (string.IsNullOrEmpty(message)) return;
// > MODERATE_MESSAGE
// This runs asynchronously and does not block the main thread
var analysis = await _client.AnalyzeTextAsync(message);
if (analysis.IsFlagged)
{
// Block the message locally and warn the player
Debug.Log($"> BLOCKED: {analysis.PrimaryCategory}");
AddMessageToChat("System", "Message blocked: Profanity or toxicity detected.", Color.red);
}
else
{
// Content is safe -> Send to game server (e.g. Photon, Mirror, Netcode)
NetworkManager.Instance.SendChatMessage(message);
// Add to local chat immediately (optimistic UI)
AddMessageToChat("You", message, Color.green);
}
chatInput.text = "";
}
private void AddMessageToChat(string username, string text, Color color)
{
string hexColor = ColorUtility.ToHtmlStringRGB(color);
chatHistory.text += $"<color=#{hexColor}><b>{username}:</b></color> {text}\n";
}
}> ARCHITECTURE_NOTE
While valid to moderate on the client for feedback, robust games should perform the final check on the server (Authoritative Server pattern) to prevent hacked clients from bypassing the filter. Our C# SDK works in both Unity clients and .NET Core backends.