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.3
gameframework_unity: ^0.0.3flutter 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 Xraph.GameFramework.Unity;
public class GameManager : FlutterMonoBehaviour
{
// The Flutter-side target name for this object.
protected override string TargetName => "GameManager";
// Handlers are registered automatically by annotating methods with
// [FlutterMethod]. This is invoked when Flutter sends
// { target: "GameManager", method: "Start" }.
[FlutterMethod("Start")]
public void OnStart(string data)
{
StartGame();
}
void StartGame()
{
// Send a message back to Flutter (the target is implicit).
SendToFlutter("GameStarted", "success");
}
}Platform Support
| Platform | Status | Requirements |
|---|---|---|
| Android | ✅ Stable | API 22+, ARM64 |
| iOS | ✅ Stable | iOS 12.0+ |
| macOS | 🟡 Beta | macOS 10.14+ |
| Windows | ⚪ Planned | Not yet supported |
| Linux | ⚪ Planned | Not yet supported |
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("MethodName", "data");JSON Messages
// Flutter
controller.sendMessage('Player', 'UpdateStats', jsonEncode({
'health': 100,
'level': 5,
}));// Unity
var stats = JsonUtility.FromJson<PlayerStats>(data);Next Steps
- SDK Overview - Learn about the SDK architecture
- Messaging API - Type-safe communication
- Examples - Sample projects