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

64 lines
1.9 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:
- **Toggle Cube**: Enables/Disables the TestCube GameObject
- **Start Timer**: Starts a 10-second countdown timer
- **Reset Scene**: Reloads the current scene
## 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
## 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