From 4b1d87365ea75f2dcf8e90ecb427873681c24889 Mon Sep 17 00:00:00 2001 From: timbotimbo Date: Sat, 19 Aug 2023 01:13:49 +0200 Subject: [PATCH 1/4] Fix AndroidJavaProxy error: assign mUnityPlayer in the MainActivity. --- .../FlutterUnityActivity.kt | 27 +++++++++++++++++++ .../flutter_unity_widget/UnityPlayerUtils.kt | 11 ++++++++ example/android/app/build.gradle | 1 + .../MainActivity.kt | 20 ++++++++++++-- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt diff --git a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt new file mode 100644 index 00000000..9f90d81b --- /dev/null +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt @@ -0,0 +1,27 @@ +package com.xraph.plugin.flutter_unity_widget + +import io.flutter.embedding.android.FlutterActivity + +/* +The following Unity versions expect the mUnityPlayer property on the main activity: +- 2020.3.46 or higher +- 2021.3.19 or higher +- 2022.2.4 or higher + +Unity will function without it, but many Unity plugins (like ARFoundation) will not. +Implement FlutterUnityActivity or the interface to fix these plugins. +*/ + +open class FlutterUnityActivity: FlutterActivity() { + @JvmField + var mUnityPlayer: java.lang.Object? = null; +} + + +/* + A function that is called when initializing Unity. + Expected use is to set a mUnityPlayer property, just as defined in FlutterUnityActivity above. +*/ +interface IFlutterUnityActivity { + fun setUnityPlayer(unityPlayer: java.lang.Object?) +} \ No newline at end of file diff --git a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/UnityPlayerUtils.kt b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/UnityPlayerUtils.kt index 266aa16b..104a9d69 100755 --- a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/UnityPlayerUtils.kt +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/UnityPlayerUtils.kt @@ -63,6 +63,17 @@ class UnityPlayerUtils { try { unityPlayer = CustomUnityPlayer(activity!!, ule) + + // Assign mUnityPlayer in the Activity, see FlutterUnityActivity.kt for more details + if(activity is FlutterUnityActivity) { + (activity!! as FlutterUnityActivity)?.mUnityPlayer = (unityPlayer as java.lang.Object?); + } else if(activity is IFlutterUnityActivity) { + (activity!! as IFlutterUnityActivity)?.setUnityPlayer(unityPlayer as java.lang.Object?); + } else { + Log.e(LOG_TAG, "Could not set mUnityPlayer in activity"); + } + + // unityPlayer!!.z = (-1).toFloat() // addUnityViewToBackground(activity!!) unityLoaded = true diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index c45c9032..ecd64b31 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -60,5 +60,6 @@ flutter { dependencies { implementation project(':unityLibrary') + implementation project(':flutter_unity_widget') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } diff --git a/example/android/app/src/main/kotlin/com/xraph/plugin/flutter_unity_widget_example/MainActivity.kt b/example/android/app/src/main/kotlin/com/xraph/plugin/flutter_unity_widget_example/MainActivity.kt index 2391ea2d..15280ffe 100644 --- a/example/android/app/src/main/kotlin/com/xraph/plugin/flutter_unity_widget_example/MainActivity.kt +++ b/example/android/app/src/main/kotlin/com/xraph/plugin/flutter_unity_widget_example/MainActivity.kt @@ -1,6 +1,22 @@ package com.xraph.plugin.flutter_unity_widget_example -import io.flutter.embedding.android.FlutterActivity +import com.xraph.plugin.flutter_unity_widget.FlutterUnityActivity; -class MainActivity: FlutterActivity() { +class MainActivity: FlutterUnityActivity() { + +} + + +// If you can't inherit FlutterUnityActivity directly, use the interface like this: +/* +import com.xraph.plugin.flutter_unity_widget.IFlutterUnityActivity; + +class ActivityExample: SomeActivity(), IFlutterUnityActivity { + @JvmField + var mUnityPlayer: java.lang.Object? = null; + + override fun setUnityPlayer(unityPlayer: java.lang.Object?) { + mUnityPlayer = unityPlayer; + } } +*/ \ No newline at end of file From d6a38111f4354c33582676ddcb5b4ca125c017b8 Mon Sep 17 00:00:00 2001 From: timbotimbo Date: Mon, 1 Jan 2024 21:38:37 +0100 Subject: [PATCH 2/4] Add MainActivity modifications to the readme. --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b1f716bf..dc520b0f 100644 --- a/README.md +++ b/README.md @@ -183,20 +183,62 @@ android { 2. Depending on your gradle version, you might need to make sure the `minSdkVersion` set in `android\app\build.gradle` matches the version that is set in Unity. Check the **Minimum API Level** setting in the Unity player settings, and match that version. -3. The Unity export script automatically sets the rest up for you. You are done with the Android setup. +3. Fixing the MainActivity. +This step is needed starting from Unity 2020.3.46+, 2021.3.19+ and 2022.2.4+. +You need a flutter_unity_widget version that is newer than 2022.2.0 for this step. + +- 3.1. Open the `android/app/build.gradle` file and add the following: + +```diff + dependencies { ++ implementation project(':flutter_unity_widget') + } +``` +- 3.2. Edit your android MainAcitivity file. +The default location for Flutter is `android/app/src/main/kotlin//MainActivity.kt`. + + If you use the default flutter activity, change it to inherit `FlutterUnityActivity`: +```diff ++ import com.xraph.plugin.flutter_unity_widget.FlutterUnityActivity; + ++ class MainActivity: FlutterUnityActivity() { +- class MainActivity: FlutterActivity() { +``` + +- 3.2. (alternative) If you use a custom or modified Activity, implement the `IFlutterUnityActivity` interface instead. + +```kotlin +// only do this if your activity does not inerhit FlutterActivity + +import com.xraph.plugin.flutter_unity_widget.IFlutterUnityActivity; + +class MainActivity: CustomActivity(), IFlutterUnityActivity { + // unity needs this mUnityPlayer property + @JvmField + var mUnityPlayer: java.lang.Object? = null; + + // implement this function so the FUW plugin can set mUnityPlayer + override fun setUnityPlayer(unityPlayer: java.lang.Object?) { + mUnityPlayer = unityPlayer; + } +} +``` + + +4. The Unity export script automatically sets the rest up for you. You are done with the Android setup. But if you want to manually set up the changes made by the export, continue.
Optional manual Android setup -4. Open the *android/settings.gradle* file and change the following: +5. Open the *android/settings.gradle* file and change the following: ```diff + include ":unityLibrary" + project(":unityLibrary").projectDir = file("./unityLibrary") ``` -5. Open the *android/app/build.gradle* file and change the following: +6. Open the *android/app/build.gradle* file and change the following: ```diff dependencies { @@ -204,7 +246,7 @@ But if you want to manually set up the changes made by the export, continue. } ``` -6. open the *android/build.gradle* file and change the following: +7. open the *android/build.gradle* file and change the following: ```diff allprojects { @@ -218,7 +260,7 @@ allprojects { } ``` -7. If you need to build a release package, open the *android/app/build.gradle* file and change the following: +8. If you need to build a release package, open the *android/app/build.gradle* file and change the following: ```diff buildTypes { @@ -239,13 +281,13 @@ allprojects { > The code above use the `debug` signConfig for all buildTypes, which can be changed as you well if you need specify signConfig. -8. If you use `minifyEnabled true` in your *android/app/build.gradle* file, open the *android/unityLibrary/proguard-unity.txt* and change the following: +9. If you use `minifyEnabled true` in your *android/app/build.gradle* file, open the *android/unityLibrary/proguard-unity.txt* and change the following: ```diff + -keep class com.xraph.plugin.** {*;} ``` -9. If you want Unity in it's own activity as an alternative, open the *android/app/src/main/AndroidManifest.xml* and change the following: +10. If you want Unity in it's own activity as an alternative, open the *android/app/src/main/AndroidManifest.xml* and change the following: ```diff + Platform specific setup (editing build.gradle and settings.gradle), replacing `unityLibrary` with `arcore_client`, `unityandroidpermissions` and `UnityARCore`. + 8. Repeat steps 5 and 6 from the Android Platform specific setup (editing build.gradle and settings.gradle), replacing `unityLibrary` with `arcore_client`, `unityandroidpermissions` and `UnityARCore`. 9. When using `UnityWidget` in Flutter, set `fullscreen: false` to disable fullscreen. From 0853589867aacef1e3a41de42ccf5276930e4e0b Mon Sep 17 00:00:00 2001 From: timbotimbo Date: Mon, 1 Jan 2024 21:43:41 +0100 Subject: [PATCH 3/4] Adjust MainAcitivty modification documentation. --- README.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dc520b0f..1ed57b32 100644 --- a/README.md +++ b/README.md @@ -183,9 +183,12 @@ android { 2. Depending on your gradle version, you might need to make sure the `minSdkVersion` set in `android\app\build.gradle` matches the version that is set in Unity. Check the **Minimum API Level** setting in the Unity player settings, and match that version. -3. Fixing the MainActivity. -This step is needed starting from Unity 2020.3.46+, 2021.3.19+ and 2022.2.4+. -You need a flutter_unity_widget version that is newer than 2022.2.0 for this step. +3. (optional) Fixing Unity plugins. +The Unity widget will function without this step, but some Unity plugins like ArFoundation will throw `mUnityPlayer` errors on newer Unity versions. + + This is needed starting from Unity 2020.3.46+, 2021.3.19+ and 2022.2.4+. +This requires a flutter_unity_widget version that is newer than 2022.2.0. + - 3.1. Open the `android/app/build.gradle` file and add the following: @@ -194,11 +197,13 @@ You need a flutter_unity_widget version that is newer than 2022.2.0 for this ste + implementation project(':flutter_unity_widget') } ``` -- 3.2. Edit your android MainAcitivity file. +- 3.2. Edit your android MainActivity file. The default location for Flutter is `android/app/src/main/kotlin//MainActivity.kt`. If you use the default flutter activity, change it to inherit `FlutterUnityActivity`: ```diff +// MainActivity.kt + + import com.xraph.plugin.flutter_unity_widget.FlutterUnityActivity; + class MainActivity: FlutterUnityActivity() { @@ -208,16 +213,18 @@ The default location for Flutter is `android/app/src/main/kotlin/ Date: Mon, 19 Feb 2024 10:16:55 +0100 Subject: [PATCH 4/4] Update some version numbers --- README.md | 4 ++-- .../plugin/flutter_unity_widget/FlutterUnityActivity.kt | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1ed57b32..45752fa9 100644 --- a/README.md +++ b/README.md @@ -186,8 +186,8 @@ Check the **Minimum API Level** setting in the Unity player settings, and match 3. (optional) Fixing Unity plugins. The Unity widget will function without this step, but some Unity plugins like ArFoundation will throw `mUnityPlayer` errors on newer Unity versions. - This is needed starting from Unity 2020.3.46+, 2021.3.19+ and 2022.2.4+. -This requires a flutter_unity_widget version that is newer than 2022.2.0. + This is needed for Unity 2020.3.46+, 2021.3.19 - 2021.3.20 and 2022.2.4 - 2022.3.18. +This requires a flutter_unity_widget version that is newer than 2022.2.1. - 3.1. Open the `android/app/build.gradle` file and add the following: diff --git a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt index 9f90d81b..442ab232 100644 --- a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt @@ -5,11 +5,13 @@ import io.flutter.embedding.android.FlutterActivity /* The following Unity versions expect the mUnityPlayer property on the main activity: - 2020.3.46 or higher -- 2021.3.19 or higher -- 2022.2.4 or higher +- 2021.3.19 - 2021.3.20 +- 2022.2.4 - 2022.3.18 Unity will function without it, but many Unity plugins (like ARFoundation) will not. Implement FlutterUnityActivity or the interface to fix these plugins. + +https://github.com/juicycleff/flutter-unity-view-widget/pull/908 */ open class FlutterUnityActivity: FlutterActivity() {