unity_widget 1.0.1+1
unity_widget: ^1.0.1+1 copied to clipboard
Embed Unity into Flutter iOS, Android and Web

unity_widget #
A Flutter plugin for embedding Unity projects in Flutter projects.
Supports Android, iOS and Web (Also supports CanvasKit).
Usage #
To use this plugin, add unity_widget as a dependency in your pubspec.yaml file.
Example #
Refer to the example project and the included Unity project.
Testing #
To test this plugin, do the following:
- Run
git clone https://github.com/Ahmadre/unity_widget.gitto create a local copy of unity_widget. - Open unity_widget in Android Studio.
Android
- Connect your Android device and run the project.
iOS
- Configure the example project and the included Unity project.
- Connect your iOS device and run the project.
Configuring your Unity project #
Android
- Go to File > Build Settings... to open the Build Settings window.
- Select Android and click Switch Platform.
- Click Add Open Scenes.
- Check Export Project.
- Click Player Settings... to open the Player Settings window.
- 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 |
- Close the Player Settings window.
- Click Export and save as
unityExport.
iOS
- Go to File > Build Settings... to open the Build Settings window.
- Select iOS and click Switch Platform.
- Click Add Open Scenes.
- Click Build and save as
UnityProject.
Configuring your Flutter project #
Android
- Copy the
unityExportfolder to<your_flutter_project>/android/unityExport. - Run
flutter pub run unity_widget:unity_export_transmogrify. - Open
<your_flutter_project>/android/unityExport/build.gradleand check ifbuildTypes { profile {} }is present. If not, add the following:
buildTypes {
profile {}
}
Refer to the example project's unityExport/build.gradle.
- Open
<your_flutter_project>/android/build.gradleand, underallprojects { repositories {} }, add the following:
flatDir {
dirs "${project(':unityExport').projectDir}/libs"
}
Refer to the example project's build.gradle.
- Open
<your_flutter_project>/android/settings.gradleand add the following:
include ':unityExport'
Refer to the example project's settings.gradle.
- Open
<your_flutter_project>/android/app/src/main/AndroidManifest.xmland add the following:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Refer to the example project's AndroidManifest.xml.
Steps 1, 2 and 3 must be repeated for every new build of the Unity project.
iOS
- Copy the
UnityProjectfolder to<your_flutter_project>/ios/UnityProjectand open<your_flutter_project>/ios/Runner.xcworkspacein Xcode. - Go to File > Add Files to "Runner"..., and add
<your_flutter_project>/ios/UnityProject/Unity-iPhone.xcodeproj. - Select
Unity-iPhone/Data, and, in the Inspectors pane, set the Target Membership to UnityFramework. - Select
Unity-iPhone, select PROJECT : Unity-iPhone, and, in the Build Settings tab, configure the following:
| Setting | Value |
|---|---|
| Build Options > Enable Bitcode | No |
| Linking > Other Linker Flags | -Wl,-U,_FlutterUnityPluginOnMessage |
- Select
Runner, select TARGETS : Runner, and, in the General tab, configure the following:
| Setting | Value | ||||
|---|---|---|---|---|---|
| Frameworks, Libraries, and Embedded Content |
|
- Select
Runner/Runner/Info.plist, and configure the following:
| Key | Type | Value |
|---|---|---|
| io.flutter.embedded_views_preview | Boolean | YES |
Steps 1, 3 and 4 must be repeated for every new build of the Unity project.
Web
-
Copy
MessageHandler.jslibfrombinfolder into your Unity-Project:MyFlutterProject/MyUnityProject/Assets/Plugins. -
In Unity Tap on your
MessageHandlerand your config should look like this:

Now tap on Apply.
Keep in mind that if you change anything in
MessageHandleryou have to rebuild your Unity WebGL Build to apply the changes for Flutter Web!
-
Build Unity for Web (WebGL) in Unity.
-
Place the exported unity WebGL build folder inside web folder.

-
Modify the index.html file inside unity WebGL build folder.
-
Add following script in index.html.
let globalUnityInstance; window["receiveMessageFromUnity"] = function (params) { window.parent.postMessage(params, "*"); }; window.parent.addEventListener("flutter2js", function (params) { const obj = JSON.parse(params.data); globalUnityInstance.SendMessage(obj.gameObject, obj.method, obj.data); });index.html file after adding the above script.

-
Add following script in index.html.
window.parent.postMessage("unity_loaded", "*"); globalUnityInstance = unityInstance;index.html file after adding the above script.

-
Tip: Save your Unity's
index.htmlandTemplateData/style.csssomewhere in your project and replace it to shorten the steps above.
Flavors #
Recommendation #
The easiest way to apply flavors for your app would be: flutter_flavorizr.
If you use flavors in your app here are the setup steps for flavored apps:
Android #
No changes needed. Flavors are applied without any additional setups.
iOS #
For your Unity iOS-Build you have to add your flavors to your Unity iOS Configuration.
- Check your actual
Runner(your app) configurations. If you have for example the flavors:
- dev
- prod
Your Runner configurations are looking like this:

So you have the flavors:
Debug-devProfile-devRelease-devDebug-prodProfile-prodRelease-prod
These flavors needs to be added to your Unity-IPhone project.
- Go into your
Unity-IPhoneproject -> PROJECTUnity-IPhone-> Info:

Here you can see in the Configurations section only:
ReleaseReleaseForProfilingReleaseForRunningDebug
- Copy
Debugconfiguration twice and rename them toDebug-devand the second toDebug-prod.
You can do that by selecting + and duplicate the configuration like this:

-
Repeat this with
ReleasetoRelease-devandRelease-prod. -
Repeat this with
ReleasetoProfile-devandProfile-prod. -
Your
Unity-IPhoneconfigurations should now look like this:

Web #
Flutter on default doesn't support --flavor for building web. But you can set your target main.dart entrypoint (with -t main.dart) while running and building. So if you setup your flavors properly there're also no changes needed for web to apply changes for your Flutter-Unity web App.
Exchanging messages between Flutter and Unity #
Flutter
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.
Unity
To send and receive messages, include FlutterUnityPlugin.cs in your project, and use the Messages.Send and Messages.Receive methods.
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.
Refer to the included Unity project's Rotate.cs.