1.9 KiB
1.9 KiB
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:
- 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:
{
"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
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