Files
UnityUDP/Example/README.md
2025-10-15 15:22:51 +02:00

100 lines
3.5 KiB
Markdown

# Unity UDP Example
This example demonstrates how to use the UnityUDP Flutter app to send commands to a Unity game.
## Setup
### Unity Setup
1. Copy `UDPCommandListener.cs` into your Unity project's `Assets/Scripts/` folder
2. Create a new empty GameObject in your scene (GameObject → Create Empty)
3. Rename it to "UDP Manager"
4. Attach the `UDPCommandListener` script to the UDP Manager GameObject
5. Set the port to 7777 (or any port you prefer) in the inspector
### Example Scene Setup
For the example commands to work, create a simple test scene:
1. Create a Cube (GameObject → 3D Object → Cube)
2. Name it "TestCube"
3. Create a UI Canvas with a Text element showing a timer (optional, for the timer command)
### Testing the Example
1. Run your Unity game
2. Open the UnityUDP Flutter app
3. Copy/move the `unityudp_projects.json` file to your Documents folder
4. Select a command and send it to your Unity game (make sure the IP address matches your computer's local IP and port is 7777)
## Sample Commands
The example includes these simple commands:
### Standard Commands (Single IP)
- **Toggle Cube**: Enables/Disables the TestCube GameObject
- **Start Timer**: Starts a 10-second countdown timer
- **Reset Scene**: Reloads the current scene
- **Rotate Cube**: Rotates the cube at different speeds
- **Change Color**: Changes the cube color (red, blue, green)
### Broadcast Commands
- **Broadcast: Toggle All Cubes**: Sends the toggle command to all Unity instances on your network
- **Broadcast: Reset All Scenes**: Resets scenes on all Unity instances on your network
### Multi-IP Commands
- **Multi-IP: Start Timers**: Sends the start timer command to multiple specific IP addresses simultaneously
## UDP Protocol
The UDP commands are sent as JSON strings with the following format:
```json
{
"command": "command_name",
"value": "optional_value"
}
```
The Unity script parses these JSON commands and executes the corresponding action.
## Customizing
You can add your own commands by:
1. Adding a new case in the `ProcessCommand` method in `UDPCommandListener.cs`
2. Creating a corresponding JSON entry in your project file
## Broadcast Mode
Broadcast mode allows you to send UDP packets to all devices on your local network simultaneously. This is useful when you have multiple Unity instances running on different computers.
### How to Use Broadcast
1. In the UnityUDP app, create or edit a package
2. Enable the "Broadcast Mode" toggle
3. Send the package - it will reach all Unity instances listening on the specified port
### Network Requirements
- All devices must be on the same local network
- Firewall must allow UDP broadcast traffic
- Unity instances must be listening on the same port
## Multiple IP Addresses
You can send the same command to multiple specific IP addresses at once:
1. In the UnityUDP app, create or edit a package
2. Add multiple IP addresses using the "Add IP" button
3. All added IPs will receive the packet when you send
This is useful for targeting specific Unity instances without broadcasting to the entire network.
## Troubleshooting
- **Commands not working**: Check that your firewall allows UDP traffic on the specified port
- **Can't connect**: Verify the IP address is correct (use `ipconfig` on Windows or `ifconfig` on Mac/Linux)
- **Unity crashes**: Make sure all GameObject names match the ones in the script
- **Broadcast not working**: Ensure your firewall allows UDP broadcast (255.255.255.255) and all devices are on the same subnet
- **Multi-IP partial failure**: Check network connectivity to each IP address individually