From 4075d916e5c23cad9b4af01f4ad3dbd8df8bd6bb Mon Sep 17 00:00:00 2001 From: timbotimbo2 <149978312+timbotimbo2@users.noreply.github.com> Date: Sun, 18 Feb 2024 19:03:45 +0100 Subject: [PATCH 1/2] Sync 2022.2.1 from pub.dev (#928) --- CHANGELOG.md | 6 +++++- pubspec.yaml | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 499bf44e..f7ae9aa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## 2022.2.0 +## 2022.2.1 + +* Minor improvements + +* ## 2022.2.0 * Enable AndroidView due to native view improvement in flutter 3.3.0 diff --git a/pubspec.yaml b/pubspec.yaml index d7b2c03d..14e0fa8f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_unity_widget description: Flutter Unity 3D widget for embedding Unity game scenes in flutter. This library now supports Unity as a Library. -version: 2022.2.0 +version: 2022.2.1 #authors: # - Rex Raphael # - Thomas Stockx @@ -8,7 +8,7 @@ version: 2022.2.0 homepage: https://github.com/juicycleff/flutter-unity-view-widget/tree/master environment: - sdk: ">=2.16.0 <3.0.0" + sdk: ">=2.16.0 <4.0.0" flutter: ">=3.3.0" dependencies: From ecee37d189647b78d395c4a793abcfc99553743f Mon Sep 17 00:00:00 2001 From: timbotimbo Date: Mon, 19 Feb 2024 10:20:43 +0100 Subject: [PATCH 2/2] [Android] Fix Unity plugins using the AndroidJavaProxy. (mUnityPlayer error) (#908) * Fix AndroidJavaProxy error: assign mUnityPlayer in the MainActivity. * Add MainActivity modifications to the readme. * Adjust MainAcitivty modification documentation. * Update some version numbers --- README.md | 65 ++++++++++++++++--- .../FlutterUnityActivity.kt | 29 +++++++++ .../flutter_unity_widget/UnityPlayerUtils.kt | 11 ++++ example/android/app/build.gradle | 1 + .../MainActivity.kt | 20 +++++- 5 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt diff --git a/README.md b/README.md index b1f716bf..45752fa9 100644 --- a/README.md +++ b/README.md @@ -183,20 +183,69 @@ 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. (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 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 + dependencies { ++ implementation project(':flutter_unity_widget') + } +``` +- 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() { +- class MainActivity: FlutterActivity() { +``` + +- 3.2. (alternative) If you use a custom or modified Activity, implement the `IFlutterUnityActivity` interface instead. + +```kotlin +// MainActivity.kt + +// only do this if your activity does not inherit FlutterActivity + +import com.xraph.plugin.flutter_unity_widget.IFlutterUnityActivity; + +class MainActivity: CustomActivity(), IFlutterUnityActivity { + // unity will try to read this mUnityPlayer property + @JvmField + var mUnityPlayer: java.lang.Object? = null; + + // implement this function so the 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 +253,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 +267,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 +288,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. 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..442ab232 --- /dev/null +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityActivity.kt @@ -0,0 +1,29 @@ +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 - 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() { + @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