A Flutter plugin for embedding Unity projects in Flutter projects.
Both Android and iOS are supported.
To use this plugin, add flutter_unity
as a dependency in your pubspec.yaml file.
flutter_unity:
git:
url: [email protected]:gatari/flutter-unity.git
ref: 1.1.0
Refer to the template project.
- In the Player Settings window, configure the following:
Setting | Value |
---|---|
Resolution and Presentation > Start in fullscreen mode | No |
Other Settings > Rendering > Graphics APIs | OpenGLES3 |
Other Settings > Configuration > Scripting Backend | IL2CPP |
Other Settings > Configuration > Target Architectures | ARMv7, ARM64 |
- Select
Build/Export Android
to export Gradle project into flutter android directory.
- Select
Build/Export IOS
to export Gradle project into flutter android directory.
- Run
flutter pub run flutter_unity:unity_export_transmogrify
. - Open
<your_flutter_project>/android/build.gradle
and, underallprojects { repositories {} }
, add the following:
flatDir {
dirs "${project(':unityExport').projectDir}/libs"
}
- Open
<your_flutter_project>/android/settings.gradle
and add the following:
include ':unityExport'
- Open
<your_flutter_project>/android/app/src/main/AndroidManifest.xml
and add the following:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Steps 1 must be repeated for every new build of the Unity project.
- Open
<your_flutter_project>/ios/Runner.xcworkspace
in Xcode. - Go to File > Add Files to "Runner"..., and add
<your_flutter_project>/ios/UnityProject/Unity-iPhone.xcodeproj
. - Select
Runner
, select TARGETS : Runner, and, in the General tab, configure the following:
Setting | Value | ||||
---|---|---|---|---|---|
Frameworks, Libraries, and Embedded Content |
|
To send a message, define the onCreated
callback in your UnityView
widget, and use the send
method from the received controller
.
To receive a message, define the onMessage
callback in your UnityView
widget.
To export from Unity editor, import Unity Package. Post build process scripts are in Editor directory to transform exported builds, making them adapt to flutter app.
"dependencies": {
...
"net.caffeineinject.flutter-unity-plugin": "https://github.com/t5ujiri/flutter-unity.git?path=example/unity/flutter_unity_example_unity/Packages/FlutterUnityPlugin"
...
}
To send messages from Unity, use Messages.Send;
To receive messages, use singleton instance of FlutterMessageReceiver.
A Message
object has the following members:
- id (
int
)
A non-negative number representing the source view when receiving a message, and the destination view when sending a message. When sending a message, it can also be set to a negative number, indicating that the message is intended for any existing view.
- data (
string
)
The actual message.