SDK DocumentationGame EnginesUnity Engine
Unity Integration Overview
The gameframework_unity package provides complete Unity Engine integration for Flutter applications.
Features
- Multi-Platform - Android, iOS, macOS, Windows, Linux
- Bidirectional Communication - Flutter ↔ Unity messaging
- Lifecycle Management - Automatic pause/resume/dispose
- Platform Views - Native rendering with Flutter overlays
- Data Streaming - Dynamic asset loading (optional)
- WebGL Support - Run Unity games in Flutter Web
Installation
dependencies:
gameframework: ^0.0.1
gameframework_unity: ^0.0.1flutter pub getBasic Setup
1. Initialize Plugin
import 'package:gameframework_unity/gameframework_unity.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
UnityEnginePlugin.initialize();
runApp(MyApp());
}2. Add GameWidget
GameWidget(
engineType: GameEngineType.unity,
config: GameEngineConfig(
runImmediately: true,
),
onEngineCreated: (controller) {
controller.sendMessage('GameManager', 'Start', '');
},
)3. Unity C# Scripts
Create a C# script that extends FlutterMonoBehaviour:
using GameFramework;
public class GameManager : FlutterMonoBehaviour
{
void Start()
{
// Listen for Flutter messages
MessageRouter.RegisterHandler("GameManager", HandleMessage);
}
void HandleMessage(string method, string data)
{
if (method == "Start")
{
StartGame();
}
}
void StartGame()
{
// Send message to Flutter
SendToFlutter("Flutter", "GameStarted", "success");
}
}Platform Support
| Platform | Status | Requirements |
|---|---|---|
| Android | ✅ Stable | API 22+, ARM64 |
| iOS | ✅ Stable | iOS 12.0+ |
| macOS | 🟡 Beta | macOS 10.14+ |
| Windows | 🟡 Beta | Windows 10+ |
| Linux | 🟡 Beta | Ubuntu 20.04+ |
Unity Project Setup
Required Unity Plugins
The Unity project needs Game Framework scripts:
# Sync scripts to Unity project
game sync scripts --project /path/to/UnityProjectThis adds:
FlutterMonoBehaviour.cs- Base class for Unity scriptsMessageRouter.cs- Message handlingBinaryMessageProtocol.cs- High-performance messaging
Build Settings
Configure Unity for Flutter integration:
- Scenes - Add required scenes to Build Settings
- Player Settings - Configure for target platforms
- Scripting Backend - Use IL2CPP for better performance
- API Level - Android API 22+ minimum
Communication
Flutter → Unity
controller.sendMessage('ObjectName', 'MethodName', 'data');Unity → Flutter
SendToFlutter("Flutter", "MethodName", "data");JSON Messages
// Flutter
controller.sendMessage('Player', 'UpdateStats', jsonEncode({
'health': 100,
'level': 5,
}));// Unity
var stats = JsonUtility.FromJson<PlayerStats>(data);Advanced Features
- Data Streaming - Dynamic asset loading
- WebGL Support - Unity in Flutter Web
- Desktop Integration - Desktop platforms
- AR Foundation - Augmented reality
See the Unity Setup Guide for detailed configuration.
Next Steps
- Unity Setup - Detailed setup guide
- Communication - Advanced messaging
- Examples - Sample projects