flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

How to export Unity to a Flutter plugin

Open laurentP22 opened this issue 10 months ago • 0 comments

Hi, I'm currently working on a Flutter app and would like to export Unity into an external Flutter plugin. The purpose of moving Unity integration to a separate plugin is to simplify our development process by:

  • Having a specific example app to test the Unity integration independently.
  • Reducing the number of files in the main app.
  • Sharing the Unity code between multiple apps.

Until now, I've been focused on the Android integration. I've found two related topics on StackOverflow, but neither solution worked for me:

What I tried:

  • Created a new Flutter plugin named "bridge": flutter create --org com.example --template=plugin --platforms=android bridge
  • Added the DemoApp Unity project from the example app of this repository to the plugin (path: bridge/unity/DemoApp).
  • Opened the DemoApp project in Unity.
  • Executed Flutter -> Export Android Release.
    • The unityLibrary folder is correctly generated (path: bridge/android/unityLibrary).
  • Updated bridge/android/settings.gradle to convert the unityLibrary folder into a module:
rootProject.name = 'bridge'

include ":unityLibrary"
project(":unityLibrary").projectDir = file("./unityLibrary")
  • Updated bridge/android/build.gradle to add the directory to the repositories:
allprojects {
    repositories {
        flatDir {
            dirs "unityLibrary/libs"
        }

        google()
        mavenCentral()
    }
}
  • Updated bridge/android/build.gradle to implement the module:
dependencies {
    implementation project(':unityLibrary')
}
  • When building the example app, it fails with:
FAILURE: Build failed with an exception.

* Where:
Build file '.../bridge/android/build.gradle' line: 76 // <-- Corresponds to `implementation project(':unityLibrary')`

* What went wrong:
A problem occurred evaluating project ':bridge'.
> Project with path ':unityLibrary' could not be found in project ':bridge'.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 381ms
Error: Gradle task assembleDebug failed with exit code 1

I tried several configurations, but it always fails to build. I'm really not an expert in Android and Gradle configuration, so I might be missing something, or not configuring the project correctly. Or maybe it's actually not possible to do...

Has anyone successfully achieved such an integration?

Thanks for your assistance!

laurentP22 avatar Feb 13 '25 08:02 laurentP22