Skip to content

Commit ecee37d

Browse files
authored
[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
1 parent 4075d91 commit ecee37d

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed

README.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,77 @@ android {
183183
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.
184184
Check the **Minimum API Level** setting in the Unity player settings, and match that version.
185185

186-
3. The Unity export script automatically sets the rest up for you. You are done with the Android setup.
186+
3. (optional) Fixing Unity plugins.
187+
The Unity widget will function without this step, but some Unity plugins like ArFoundation will throw `mUnityPlayer` errors on newer Unity versions.
188+
189+
This is needed for Unity 2020.3.46+, 2021.3.19 - 2021.3.20 and 2022.2.4 - 2022.3.18.
190+
This requires a flutter_unity_widget version that is newer than 2022.2.1.
191+
192+
193+
- 3.1. Open the `android/app/build.gradle` file and add the following:
194+
195+
```diff
196+
dependencies {
197+
+ implementation project(':flutter_unity_widget')
198+
}
199+
```
200+
- 3.2. Edit your android MainActivity file.
201+
The default location for Flutter is `android/app/src/main/kotlin/<app identifier>/MainActivity.kt`.
202+
203+
If you use the default flutter activity, change it to inherit `FlutterUnityActivity`:
204+
```diff
205+
// MainActivity.kt
206+
207+
+ import com.xraph.plugin.flutter_unity_widget.FlutterUnityActivity;
208+
209+
+ class MainActivity: FlutterUnityActivity() {
210+
- class MainActivity: FlutterActivity() {
211+
```
212+
213+
- 3.2. (alternative) If you use a custom or modified Activity, implement the `IFlutterUnityActivity` interface instead.
214+
215+
```kotlin
216+
// MainActivity.kt
217+
218+
// only do this if your activity does not inherit FlutterActivity
219+
220+
import com.xraph.plugin.flutter_unity_widget.IFlutterUnityActivity;
221+
222+
class MainActivity: CustomActivity(), IFlutterUnityActivity {
223+
// unity will try to read this mUnityPlayer property
224+
@JvmField
225+
var mUnityPlayer: java.lang.Object? = null;
226+
227+
// implement this function so the plugin can set mUnityPlayer
228+
override fun setUnityPlayer(unityPlayer: java.lang.Object?) {
229+
mUnityPlayer = unityPlayer;
230+
}
231+
}
232+
```
233+
234+
235+
4. The Unity export script automatically sets the rest up for you. You are done with the Android setup.
187236
But if you want to manually set up the changes made by the export, continue.
188237

189238
<details>
190239
<summary> Optional manual Android setup </summary>
191240

192-
4. Open the *android/settings.gradle* file and change the following:
241+
5. Open the *android/settings.gradle* file and change the following:
193242

194243
```diff
195244
+ include ":unityLibrary"
196245
+ project(":unityLibrary").projectDir = file("./unityLibrary")
197246
```
198247

199-
5. Open the *android/app/build.gradle* file and change the following:
248+
6. Open the *android/app/build.gradle* file and change the following:
200249

201250
```diff
202251
dependencies {
203252
+ implementation project(':unityLibrary')
204253
}
205254
```
206255

207-
6. open the *android/build.gradle* file and change the following:
256+
7. open the *android/build.gradle* file and change the following:
208257

209258
```diff
210259
allprojects {
@@ -218,7 +267,7 @@ allprojects {
218267
}
219268
```
220269

221-
7. If you need to build a release package, open the *android/app/build.gradle* file and change the following:
270+
8. If you need to build a release package, open the *android/app/build.gradle* file and change the following:
222271

223272
```diff
224273
buildTypes {
@@ -239,13 +288,13 @@ allprojects {
239288

240289
> The code above use the `debug` signConfig for all buildTypes, which can be changed as you well if you need specify signConfig.
241290
242-
8. If you use `minifyEnabled true` in your *android/app/build.gradle* file, open the *android/unityLibrary/proguard-unity.txt* and change the following:
291+
9. If you use `minifyEnabled true` in your *android/app/build.gradle* file, open the *android/unityLibrary/proguard-unity.txt* and change the following:
243292

244293
```diff
245294
+ -keep class com.xraph.plugin.** {*;}
246295
```
247296

248-
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:
297+
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:
249298

250299
```diff
251300
+ <activity
@@ -341,7 +390,7 @@ Switch to version `4.2.2` or `4.4` to avoid this.
341390
There seems to be a bug where a Unity export does not include all lib files. If they are missing, use Unity to build a standalone .apk
342391
of your AR project, unzip the resulting apk, and copy over the missing .lib files to the `unityLibrary` module.
343392

344-
8. Repeat steps 4 and 5 from the Android <b>Platform specific setup</b> (editing build.gradle and settings.gradle), replacing `unityLibrary` with `arcore_client`, `unityandroidpermissions` and `UnityARCore`.
393+
8. Repeat steps 5 and 6 from the Android <b>Platform specific setup</b> (editing build.gradle and settings.gradle), replacing `unityLibrary` with `arcore_client`, `unityandroidpermissions` and `UnityARCore`.
345394

346395
9. When using `UnityWidget` in Flutter, set `fullscreen: false` to disable fullscreen.
347396

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.xraph.plugin.flutter_unity_widget
2+
3+
import io.flutter.embedding.android.FlutterActivity
4+
5+
/*
6+
The following Unity versions expect the mUnityPlayer property on the main activity:
7+
- 2020.3.46 or higher
8+
- 2021.3.19 - 2021.3.20
9+
- 2022.2.4 - 2022.3.18
10+
11+
Unity will function without it, but many Unity plugins (like ARFoundation) will not.
12+
Implement FlutterUnityActivity or the interface to fix these plugins.
13+
14+
https://github.com/juicycleff/flutter-unity-view-widget/pull/908
15+
*/
16+
17+
open class FlutterUnityActivity: FlutterActivity() {
18+
@JvmField
19+
var mUnityPlayer: java.lang.Object? = null;
20+
}
21+
22+
23+
/*
24+
A function that is called when initializing Unity.
25+
Expected use is to set a mUnityPlayer property, just as defined in FlutterUnityActivity above.
26+
*/
27+
interface IFlutterUnityActivity {
28+
fun setUnityPlayer(unityPlayer: java.lang.Object?)
29+
}

android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/UnityPlayerUtils.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ class UnityPlayerUtils {
6363

6464
try {
6565
unityPlayer = CustomUnityPlayer(activity!!, ule)
66+
67+
// Assign mUnityPlayer in the Activity, see FlutterUnityActivity.kt for more details
68+
if(activity is FlutterUnityActivity) {
69+
(activity!! as FlutterUnityActivity)?.mUnityPlayer = (unityPlayer as java.lang.Object?);
70+
} else if(activity is IFlutterUnityActivity) {
71+
(activity!! as IFlutterUnityActivity)?.setUnityPlayer(unityPlayer as java.lang.Object?);
72+
} else {
73+
Log.e(LOG_TAG, "Could not set mUnityPlayer in activity");
74+
}
75+
76+
6677
// unityPlayer!!.z = (-1).toFloat()
6778
// addUnityViewToBackground(activity!!)
6879
unityLoaded = true

example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ flutter {
6060

6161
dependencies {
6262
implementation project(':unityLibrary')
63+
implementation project(':flutter_unity_widget')
6364
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
6465
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package com.xraph.plugin.flutter_unity_widget_example
22

3-
import io.flutter.embedding.android.FlutterActivity
3+
import com.xraph.plugin.flutter_unity_widget.FlutterUnityActivity;
44

5-
class MainActivity: FlutterActivity() {
5+
class MainActivity: FlutterUnityActivity() {
6+
7+
}
8+
9+
10+
// If you can't inherit FlutterUnityActivity directly, use the interface like this:
11+
/*
12+
import com.xraph.plugin.flutter_unity_widget.IFlutterUnityActivity;
13+
14+
class ActivityExample: SomeActivity(), IFlutterUnityActivity {
15+
@JvmField
16+
var mUnityPlayer: java.lang.Object? = null;
17+
18+
override fun setUnityPlayer(unityPlayer: java.lang.Object?) {
19+
mUnityPlayer = unityPlayer;
20+
}
621
}
22+
*/

0 commit comments

Comments
 (0)