juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.13k stars 518 forks source link

Experimental Unity 6 (6000) support for Android. #967

Open timbotimbo opened 3 months ago

timbotimbo commented 3 months ago

Description

Unity 2023.1 and 6 (6000.0) include breaking changes for the Android version of this plugin. And given that 2023.3 turned into 6000, I decided to implement support for Unity 6000.

For a litte more info, see the PR https://github.com/juicycleff/flutter-unity-view-widget/pull/966

Installing & testing

This alpha version is now live in the experimental/unity_6000 branch.

You can either download it and include the plugin using a relative path

flutter_unity_widget:
    path: <folder path>

or include it using the git url

flutter_unity_widget:
    git:
      url: https://github.com/juicycleff/flutter-unity-view-widget.git
      ref: experimental/unity_6000 # branch name

Setup

  1. Your Flutter project will need to use Java 17 and gradle 8.x, Check the example project in the unity_6000 branch for the relevant build.gradle files.

  2. Set the Unity Application Entry Point to Activity. Player Settings -> Settings for Android -> Other Settings -> Application Entry Point

  3. After the Unity export, android/unityLibrary/build.gradle might include a broken import.

    • Either fix this automatically by updating your Assets/FlutterUnityIntegration/Editor/build.cs file with the one from the example project.
    • Or comment the 2nd line apply from: '../shared/keepUnitySymbols.gradle' in unityLibrary/build.gradle
artificerchris commented 1 month ago

I'm getting this error when trying to build my flutter application with the export for Android:

Build file '<flutter project>/android/unityLibrary/build.gradle' line: 57

* What went wrong:
A problem occurred evaluating project ':unityLibrary'.
> Could not find method packaging() for arguments [build_e9lpdzw6rpzcgazd3rj2x5k7f$_run_closure2$_closure12@5afe6187] on extension 'android' of type com.android.build.gradle.LibraryExtension.

Coming from this set of lines in the build.grade:

packaging {
    jniLibs {
        useLegacyPackaging true
    }
}
timbotimbo commented 1 month ago

@artificerchris

Just tried this again with Unity 6000.0.16 and Flutter 3.24.1.

The example from this repo worked worked without modifications. (I just had to delete a themeselector line from the androidmanifest)

Then I made a new project using Flutter create, and got your error.

Matching the gradle, AGP and kotlin versions with the example got me past the error. You likely forgot to upgrade the project to gradle and AGP 8.x.

Using the new gradle apply method used in the latest Flutter versions:

gradle-wrapper.properties

- distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
+ distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip

settings.gradle

-    id "com.android.application" version "7.3.0" apply false
-    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
+    id "com.android.application" version "8.3.0" apply false
+    id "org.jetbrains.kotlin.android" version "1.9.10" apply false
artificerchris commented 1 month ago

Seems my reading comprehension was a bit off, thank you for your help, that did fix that issue, and now I have a whole new one, the build works, all the C++ compiles successfully, but I get this error during my build:

e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:7:27 Unresolved reference: UnityPlayerActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:11:31 Unresolved reference: UnityPlayerActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:14:5 'onCreate' overrides nothing
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:15:15 Unresolved reference: onCreate
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:17:14 Unresolved reference: window
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:18:22 Unresolved reference: intent
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:25:9 Unresolved reference: mUnityPlayer
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:30:9 Unresolved reference: mUnityPlayer
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:34:22 None of the following functions can be called with the arguments supplied: 
public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:36:16 Variable expected
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:37:9 Unresolved reference: startActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:40:5 'onUnityPlayerUnloaded' overrides nothing

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flutter_unity_widget:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* 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 5m 59s
Error: Gradle task assembleDebug failed with exit code 1
timbotimbo commented 1 month ago

In Unity try Player Settings -> Settings for Android -> Other Settings -> Application Entry Point Check Activity and not GameActivity.

artificerchris commented 1 month ago

@timbotimbo That did it! Thank you so much for your help, my application now compiles properly for both iOS and Android. Would it be best if I run into more issues to just reply to this issue, or should I make new issues for other things I run into in the 6000 branch?