A supplementary plugin for embedding Unity into Flutter applications in a native manner.
This plugin is not intended to be used standalone.
It is designed to complement the flutter_unity_widget plugin by addressing common issues such as memory leaks and unstable touch events.
Currently, message communication continues to rely on the flutter_unity_widget package, while native-level support is provided for Android and iOS.
- Provide native-level support for more stable Unity rendering inside Flutter apps
- On Android, run
UnityActivityin a separate process (:unity) to minimize crashes in the main app - On iOS, run Unity in a separate UIViewController-based screen to minimize crashes in the main app
- (Future goals)
- Enhance the bi-directional communication bridge
- Remove dependency on flutter_unity_widget
- Transition into a standalone plugin
dependencies:
flutter_unity_widget: # or the latest version
git:
url: https://github.com/kilroy80/flutter-unity-view-widget
ref: '6bf900b5c297d3bf6cbcca8af32c0af2dbe8d7ae'
flutter_native_unity:
git:
url: https://github.com/kilroy80/flutter_native_unity.gitFollow the same configuration steps described on the
flutter_unity_widget plugin page.
// build.gradle
dependencies {
compileOnly rootProject.findProject(":flutter_native_unity")
}// build.gradle.kts
dependencies {
rootProject.findProject(":flutter_native_unity")?.let {
compileOnly(it)
}
}<application>
<activity
android:name=".ExampleUnityActivity"
android:process=":unity" />
</application>
⚠️ Important
Theunity-classes.jarfile located at${plugin-path}/android/libsmust be replaced with the JAR file that matches your Unity build version.
If the versions do not match, the Android app may crash immediately on launch.
No additional configuration is required for iOS.
FlutterNativeUnity().launch() function parameters:
initData
A JSON string containing initialization data passed when the Activity or ViewController is createdclassName
The class name of the native Activity (Android) or ViewController (iOS) to launch
import 'package:flutter_native_unity/flutter_native_unity.dart';
MaterialButton(
onPressed: () {
FlutterNativeUnity().launch(
initData: 'json_to_string',
className: Platform.isAndroid
? 'ExampleUnityActivity'
: 'ExampleViewController',
);
},
child: const Text('Open Unity Screen'),
);Under ${plugin-path}/android/src,
extend the NativeUnityActivity class and implement your own Activity in Kotlin or Java.
Refer to the ExampleUnityActivity sample code for details.
Under ${plugin-path}/ios/Classes,
extend the NativeUnityViewController class and implement your own ViewController in Swift.
Refer to the ExampleViewController sample code for details.
MIT License