Unity UDP Example
This example demonstrates how to use the UnityUDP Flutter app to send commands to a Unity game.
Setup
Unity Setup
- Copy
UDPCommandListener.csinto your Unity project'sAssets/Scripts/folder - Create a new empty GameObject in your scene (GameObject → Create Empty)
- Rename it to "UDP Manager"
- Attach the
UDPCommandListenerscript to the UDP Manager GameObject - 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:
- Create a Cube (GameObject → 3D Object → Cube)
- Name it "TestCube"
- Create a UI Canvas with a Text element showing a timer (optional, for the timer command)
Testing the Example
- Run your Unity game
- Open the UnityUDP Flutter app
- Copy/move the
unityudp_projects.jsonfile to your Documents folder - 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:
{
"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:
- Adding a new case in the
ProcessCommandmethod inUDPCommandListener.cs - 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
- In the UnityUDP app, create or edit a package
- Enable the "Broadcast Mode" toggle
- 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:
- In the UnityUDP app, create or edit a package
- Add multiple IP addresses using the "Add IP" button
- 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
ipconfigon Windows orifconfigon 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