Skip to content

Commit

Permalink
Merge pull request juicycleff#1 from thomas-stockx/feature/ar
Browse files Browse the repository at this point in the history
Add Unity AR Foundation support.
  • Loading branch information
thomas-stockx authored Jul 31, 2019
2 parents a838b4b + dfd1976 commit 5277f94
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 64 deletions.
9 changes: 0 additions & 9 deletions .idea/libraries/Flutter_Plugins.xml

This file was deleted.

32 changes: 0 additions & 32 deletions .idea/misc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/vcs.xml

This file was deleted.

17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ IOS will export unity project to `ios/UnityExport`.
```

<br />

### AR Foundation (ANDROID only at the moment)
If you want to use Unity for integrating Augmented Reality in your Flutter app, a few more changes are required:
1. Export the Unity Project as previously stated (using the Editor Build script).
2. Check if the exported project includes all required Unity libraries (.so) files (`lib/\<architecture\>/libUnityARCore.so` and `libarpresto_api.so`). 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 of your AR project, unzip the resulting apk, and copy over the missing .lib files to the `UnityExport` module.
3. Similar to how you've created the `unity-classes` module in Android Studio, create similar modules for all exported .aar and .jar files in the `UnityExport/libs` folder (`arcore_client.aar`, `unityandroidpermissions.aar`, `UnityARCore.aar`).
4. Update the build.gradle script of the `UnityExport` module to depend on the new modules (again, similar to how it depends on `unity-classes`).
5. Finally, update your Dart code build method where you include the `UnityWidget` and add `isARScene: true,`.
Sadly, this does have the side effect of making your Flutter activity act in full screen, as Unity requires control of your Activity for running in AR, and it makes several modifications to your Activity as a result (including setting it to full screen).


### Add UnityMessageManager Support

Expand Down Expand Up @@ -266,8 +276,11 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
## API
- pause()

## Known issues and their fix
- Android Export gradle issues
## Known issues
- no iOS support yet
- Android Export requires several manual changes
- Using AR will make the activity run in full screen (hiding status and navigation bar).


[version-badge]: https://img.shields.io/pub/v/flutter_unity_widget.svg?style=flat-square
[package]: https://pub.dartlang.org/packages/flutter_unity_widget/versions/0.1.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ public class FlutterUnityView implements PlatformView, MethodChannel.MethodCallH
public void onMethodCall(MethodCall methodCall, final MethodChannel.Result result) {
switch (methodCall.method) {
case "createUnity":
String isAR;
isAR = methodCall.argument("isAR");

if (isAR != null) {
UnityUtils.isAR = true;
}

UnityUtils.createPlayer(registrar.activity(), new UnityUtils.CreateCallback() {
@Override
public void onReady() {
result.success(true);
result.success(true);
}
});


break;
case "isReady":
result.success(UnityUtils.isUnityReady());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Context;

// import io.flutter.plugin.common.BinaryMessenger;
import java.util.Map;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
Expand All @@ -22,7 +24,13 @@ public FlutterUnityViewFactory(PluginRegistry.Registrar registrar) {
}

@Override
public PlatformView create(Context context, int i, Object o) {
public PlatformView create(Context context, int i, Object args) {
Map<String, Object> params = (Map<String, Object>) args;

if (params.containsKey("ar")) {
UnityUtils.isAR = (boolean) params.get("ar");
}

return new FlutterUnityView(context, mPluginRegistrar, i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface CreateCallback {
void onReady();
}

public static boolean isAR = false;

private static UnityPlayer unityPlayer;
private static boolean _isUnityReady;
private static boolean _isUnityPaused;
Expand Down Expand Up @@ -50,7 +52,7 @@ public static void createPlayer(final Activity activity, final CreateCallback ca
public void run() {
activity.getWindow().setFormat(PixelFormat.RGBA_8888);

unityPlayer = new UnityPlayer((ContextWrapper) activity.getApplicationContext());
unityPlayer = new UnityPlayer(isAR ? activity : activity.getApplicationContext());

try {
// wait a moument. fix unity cannot start when startup.
Expand Down
8 changes: 7 additions & 1 deletion lib/flutter_unity_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ class UnityWidget extends StatefulWidget {
///Event fires when the [UnityWidget] gets a message from unity.
final onUnityMessageCallback onUnityMessage;

final bool isARScene;

UnityWidget(
{Key key, @required this.onUnityViewCreated, this.onUnityMessage});
{Key key, @required this.onUnityViewCreated, this.onUnityMessage, this.isARScene = false});

@override
_UnityWidgetState createState() => _UnityWidgetState();
Expand All @@ -110,11 +112,15 @@ class _UnityWidgetState extends State<UnityWidget> {

@override
Widget build(BuildContext context) {
final Map<String, dynamic> creationParams = <String, dynamic>{
'ar': widget.isARScene,
};
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: 'unity_view',
onPlatformViewCreated: _onPlatformViewCreated,
creationParamsCodec: const StandardMessageCodec(),
creationParams: creationParams,
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
Expand Down

0 comments on commit 5277f94

Please sign in to comment.