Files
Master-Arbeit-Tom-Hempel/Unity-Master/Assets/Scripts/LocalTestingSetup.md
2025-09-21 22:42:26 +02:00

142 lines
4.1 KiB
Markdown

# Local Testing Setup Guide
## Testing Both Players in Same Scene
When testing the UDP avatar sync system with both broadcaster and receiver in the same Unity scene, you need to handle port conflicts properly.
### The Port Conflict Issue
The error you encountered:
```
Failed to start UDP listener: Only one usage of each socket address (protocol/network address/port) is normally permitted.
```
This happens because both components try to bind to the same UDP port (8080) simultaneously.
## Solution: Updated Receiver with Port Sharing
The `UDPAvatarReceiver` has been updated with port sharing capabilities:
### New Settings in Inspector
- **Allow Port Sharing**: ✅ Enable this for local testing
- **Listen Port**: 8080 (or will auto-find alternative)
### How It Works
1. **Port Sharing Enabled**: Uses `SO_REUSEADDR` socket option to allow multiple bindings
2. **Fallback Ports**: If port sharing fails, tries ports 8081, 8082, 8083, etc.
3. **Debug Info**: Shows actual port being used in the GUI
## Local Testing Setup
### GameObject 1: Player 1 (Broadcaster + Receiver)
```
Avatar Player 1
├── UDPAvatarBroadcaster
│ ├── Player ID: 1
│ ├── Avatar Root: [Player 1 Avatar]
│ └── Show Debug Info: ✅
└── UDPAvatarReceiver
├── Target Player ID: 2 (receive from Player 2)
├── Target Avatar Root: [Player 2 Avatar Clone]
├── Allow Port Sharing: ✅
└── Show Debug Info: ✅
```
### GameObject 2: Player 2 (Broadcaster + Receiver)
```
Avatar Player 2
├── UDPAvatarBroadcaster
│ ├── Player ID: 2
│ ├── Avatar Root: [Player 2 Avatar]
│ └── Show Debug Info: ✅
└── UDPAvatarReceiver
├── Target Player ID: 1 (receive from Player 1)
├── Target Avatar Root: [Player 1 Avatar Clone]
├── Allow Port Sharing: ✅
└── Show Debug Info: ✅
```
## Alternative Setup Methods
### Method 1: Different Ports (Simple)
- Player 1 Broadcaster: Port 8080
- Player 2 Broadcaster: Port 8081
- Update receivers to listen on corresponding ports
### Method 2: Single Broadcaster (Minimal)
- One broadcaster component sending data
- Multiple receivers in scene listening
- Good for testing receiver logic only
### Method 3: Separate Scenes (Real-world)
- Build and run multiple instances of your game
- Most accurate representation of real multiplayer
## Troubleshooting
### Still Getting Port Errors?
1. **Check Windows Firewall**:
```
Allow Unity.exe through Windows Defender Firewall
Allow UDP port 8080-8085
```
2. **Verify Port Sharing Setting**:
```
UDPAvatarReceiver → Allow Port Sharing: ✅
```
3. **Check Console for Alternative Ports**:
```
Look for: "UDP listener started on alternative port 8081"
```
### No Data Being Received?
1. **Verify Player IDs**:
- Broadcaster Player ID: 1
- Receiver Target Player ID: 1 (to receive from broadcaster)
2. **Check Debug GUI**:
- Broadcaster: "Broadcasting" status
- Receiver: "Listen Port" shows actual port
3. **Test Loopback**:
- Set Receiver Target Player ID: 0 (receive from any)
- Should receive own broadcasts
## Debug Information
Enable debug info on both components to see:
**Broadcaster GUI (Top-left)**:
- Player ID and broadcast status
- Sequence number (should increment)
- Update rate and packet size
**Receiver GUI (Top-right)**:
- Listen port (actual port being used)
- Packets received/dropped
- Target player ID
## Production vs Testing
| Environment | Setup | Port Handling |
|-------------|-------|---------------|
| **Local Testing** | Same scene, port sharing | Allow Port Sharing: ✅ |
| **LAN/Hotspot** | Separate devices | Standard UDP binding |
| **Production** | Different machines | No port conflicts |
## Quick Checklist
1. ✅ Enable "Allow Port Sharing" on all receivers
2. ✅ Set unique Player IDs (1, 2, 3...)
3. ✅ Set correct Target Player IDs on receivers
4. ✅ Enable debug info to monitor status
5. ✅ Check console for port conflict messages
6. ✅ Verify Windows Firewall allows Unity/UDP
The updated receiver should now handle local testing scenarios gracefully!