Unity TCP Bridge is a lightweight TCP socket server implementation for Unity Editor that provides networking capabilities with port management and client handling.
- Modern Architecture: Built with async/await patterns for Unity Editor
- Automatic Port Management: Smart port allocation with project-specific persistence
- Client Connection Handling: Multi-client support with proper connection management
- Socket Configuration: Optimized socket options for reliable connections
- Dynamic Port Allocation: Automatically finds available ports starting from default (25916)
- Project-Specific Storage: Remembers port settings per Unity project
- Port Conflict Resolution: Handles port conflicts gracefully with fallback options
- Cross-Platform Support: Works on Windows, macOS, and Linux
- Keep-Alive Support: Configurable socket keep-alive for stable connections
- Connection Timeout: Prevents hanging connections with configurable timeouts
- Multi-Client Support: Handles multiple simultaneous client connections
- Graceful Shutdown: Proper cleanup on Unity Editor shutdown or assembly reload
- Control Window: Full-featured TCP bridge management interface (
Tools > Unity TCP Bridge > Control Window) - Status Window: Compact dockable status monitor (
Tools > Unity TCP Bridge > Status Window) - Menu Items: Quick actions via Unity menu (
Tools > Unity TCP Bridge) - Real-time Status: Live server status updates and port information
- Debug Controls: Toggle debug logging and port management tools
- Unity 2021.3 or later
- Add this package to your Unity project
- The TCP server will start automatically when Unity loads
- Check Unity Console for server startup confirmation and port information
The TCP server starts automatically when Unity loads and listens for client connections. By default:
- Default Port: 25916 (automatically managed)
- Protocol: Basic TCP with welcome message
- Echo Server: Currently configured as an echo server (sends received data back to client)
Access the TCP Bridge controls through Unity's menu system:
-
Control Window:
Tools > Unity TCP Bridge > Control Window- Complete server management interface
- Real-time status monitoring
- Server start/stop/restart controls
- Port management and configuration
- Debug logging controls
-
Status Window:
Tools > Unity TCP Bridge > Status Window- Compact status display (can be docked)
- Quick start/stop buttons
- Shows current port and connection status
-
Menu Actions:
Tools > Unity TCP Bridge > [Action]- Quick server operations
- Port discovery and management
- Debug logging toggle
# Example using telnet (replace 25916 with actual port shown in Unity Console or UI)
telnet localhost 25916The current implementation includes a basic echo server. To customize for your needs:
- Modify the
HandleClientAsyncmethod inUnityTcpBridge.cs - Replace the echo logic with your custom protocol handling
- Update the welcome message format if needed
// Get the current port
int port = UnityTcpBridge.GetCurrentPort();
// Check if server is running
bool isRunning = UnityTcpBridge.IsRunning;
// Manually start/stop (usually automatic)
UnityTcpBridge.Start();
UnityTcpBridge.Stop();External TCP Client β Unity TCP Server β Custom Protocol Handler
- UnityTcpBridge: Main TCP server with lifecycle management
- PortManager: Dynamic port allocation and persistence
- TcpLog: Logging utility for TCP operations
- Async Operations: Non-blocking I/O operations
- Connection Pooling: Efficient client connection management
- Resource Cleanup: Automatic cleanup on Unity lifecycle events
Enable detailed logging for troubleshooting:
// In Unity Editor, set this EditorPref:
EditorPrefs.SetBool("UnityTcp.DebugLogs", true);The server automatically manages ports, but you can access port management:
// Check if a port is available
bool available = PortManager.IsPortAvailable(25916);
// Get port with fallback
int port = PortManager.GetPortWithFallback();
// Discover a new port
int newPort = PortManager.DiscoverNewPort();- Clone this repository
- Open in Unity 2021.3 or later
- The TCP server will start automatically
- Check Unity Console for server status
To implement your custom TCP protocol:
- Modify HandleClientAsync: Replace the echo server logic
- Update Welcome Message: Change the initial handshake
- Add Message Parsing: Implement your message format
- Error Handling: Add appropriate error handling for your protocol
Example customization:
// In HandleClientAsync method, replace echo logic with:
while (!token.IsCancellationRequested && client.Connected)
{
// Read client message
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, token);
if (bytesRead == 0) break;
// Parse your custom message format
string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// Process the message according to your protocol
string response = ProcessCustomMessage(message);
// Send response
byte[] responseBytes = Encoding.UTF8.GetBytes(response);
await stream.WriteAsync(responseBytes, 0, responseBytes.Length, token);
}Port Already in Use
- The server will automatically find an alternative port
- Check Unity Console for the actual port being used
- Use
PortManager.DiscoverNewPort()to force a new port
Server Won't Start
- Verify Unity project is loaded properly
- Check Unity Console for error messages
- Ensure no firewall is blocking the port
Client Can't Connect
- Verify the server is running (check Unity Console)
- Use the correct port number shown in Unity Console
- Ensure localhost connectivity
Connection Drops
- Check if client is sending data within timeout period (60 seconds default)
- Enable debug logging to see connection details
- Verify client is properly handling the welcome message
Enable detailed logging to get comprehensive information about:
- Server startup and port allocation
- Client connections and disconnections
- Data transmission and errors
- Port management decisions
This project is licensed under the MIT License.