From c2a2318c1488f0dc1d5470ee55fe69da7dd46204 Mon Sep 17 00:00:00 2001 From: ricardodalarme Date: Fri, 22 Sep 2023 12:31:59 -0300 Subject: [PATCH 1/5] Remove ndkPath from generated build.gradle - Starting Unity 2023, Unity generates build.gradle with ndkPath which contains the absolute path to the NDK (e.g. `ndkPath "/Applications/Unity/Hub/Editor/2023.1.14f1/PlaybackEngines/AndroidPlayer/NDK"`). This causes the build to fail when tested on a different machine or when the ndk version is defined in the app level build.gradle. --- .../unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs b/example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs index 411c2cc1..7b2ceba2 100644 --- a/example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs +++ b/example/unity/DemoApp/Assets/FlutterUnityIntegration/Editor/Build.cs @@ -330,6 +330,7 @@ private static void ModifyAndroidGradle(bool isPlugin) buildText = buildText.Replace("enableSplit = true", "enable true"); buildText = buildText.Replace("implementation fileTree(dir: 'libs', include: ['*.jar'])", "implementation(name: 'unity-classes', ext:'jar')"); buildText = buildText.Replace(" + unityStreamingAssets.tokenize(', ')", ""); + buildText = Regex.Replace(buildText, "ndkPath \".*\"", ""); if(isPlugin) { From 96d3bda4e0597554c3f967c1152656d4966717ba Mon Sep 17 00:00:00 2001 From: TonyHoyleRps <34508505+TonyHoyleRps@users.noreply.github.com> Date: Sun, 5 Nov 2023 23:41:16 +0000 Subject: [PATCH 2/5] Web: Add basepath to UnityLibrary URI (#865) --- lib/src/web/web_unity_widget_view.dart | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/src/web/web_unity_widget_view.dart b/lib/src/web/web_unity_widget_view.dart index 6c6eb8bd..6d8d2f35 100644 --- a/lib/src/web/web_unity_widget_view.dart +++ b/lib/src/web/web_unity_widget_view.dart @@ -18,7 +18,7 @@ class WebUnityWidgetView extends StatefulWidget { class _WebUnityWidgetViewState extends State { final WebViewController _controller = WebViewController() ..loadRequest( - Uri.parse('${Uri.base.origin}/UnityLibrary/index.html'), + Uri.parse('${_getBasePath()}/UnityLibrary/index.html'), ); @override @@ -36,4 +36,10 @@ class _WebUnityWidgetViewState extends State { Widget build(BuildContext context) { return WebViewWidget(controller: _controller); } + + static String _getBasePath() { + var prefix = Uri.base.origin+Uri.base.path; + if (prefix.endsWith("/")) prefix = prefix.substring(0, prefix.length - 1); + return prefix; + } } From d2382fbd439a86faba8d8ac71250816b26b17128 Mon Sep 17 00:00:00 2001 From: timbotimbo2 <149978312+timbotimbo2@users.noreply.github.com> Date: Mon, 6 Nov 2023 01:03:25 +0100 Subject: [PATCH 3/5] Fix bad formatting in merged commit. --- lib/src/web/web_unity_widget_view.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/web/web_unity_widget_view.dart b/lib/src/web/web_unity_widget_view.dart index 6d8d2f35..63cb6bbb 100644 --- a/lib/src/web/web_unity_widget_view.dart +++ b/lib/src/web/web_unity_widget_view.dart @@ -36,9 +36,9 @@ class _WebUnityWidgetViewState extends State { Widget build(BuildContext context) { return WebViewWidget(controller: _controller); } - + static String _getBasePath() { - var prefix = Uri.base.origin+Uri.base.path; + var prefix = Uri.base.origin + Uri.base.path; if (prefix.endsWith("/")) prefix = prefix.substring(0, prefix.length - 1); return prefix; } 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 4/5] 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 5/5] [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