Links

Quickstart in Flutter

Integrate the ARwayKit SDK with Flutter
In this quickstart guide, we will be integrating the ARwayKit SDK in the standard flutter app

Installed Versions

In this guide, we will be using the following versions of Flutter, Dart, and Unity:
  • Flutter version: 3.7.7
  • Dart version: 2.19.4
  • Unity: 2021.3.1f1 or Above (LTS)

Download the ARwayKit SDK

Steps
  1. 1.
    Download the ARwayKit SDK project from GitHub as a ZIP. You can contact us for access to the ARwayKit SDK.

Creating a Blank Project in Flutter

If you have already created a Flutter project, you may skip the installation instructions and proceed with the next steps. Otherwise, kindly refer to the Flutter documentation for installation guidelines.
flutter create my_project_name

Configure the Flutter Project

We are using the flutter_unity_widget package to integrate our ARwayKit SDK with Flutter. https://pub.dev/packages/flutter_unity_widget
Steps
  1. 1.
    Open the pubspec.yaml file and add flutter_unity_widget to it under the dependencies section.
    dependencies:
    flutter_unity_widget: ^2022.2.0
    • Now inside your Dart code you can import it.
      import 'package:flutter_unity_widget/flutter_unity_widget.dart';
  2. 2.
    After adding the dependencies, you need to fetch them into your Flutter project. You can fetch the dependencies by -
    • From the terminal: Run flutter pub get OR
    • From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.
    • From VS Code: Click Get Packages located on the right side of the action ribbon at the top of pubspec.yaml.
  3. 3.
    Then add an import statement for using the package inside your Dart code.
    We have created sample Dart files with unity pre-integrated to help you get started quickly with the project. The Dart files are located in the lib folder within the Flutter project.

Configure the flutter_unity_widget Package

Configuring the flutter_unity_widget can be done by following the instructions in their README.md, or by following the steps outlined below.
Make sure to add the Account ID and Secret Key variables to the Unity project. Follow the guide for Integrating Unity Packages for instructions.

Prerequisites

NDK
If your project requires Android NDK, you have to setup following:
Your android project needs to know the path of the NDK Unity uses. You can find the path to the NDK under Preferences -> External Tools:
Copy the path by right click and paste the path here in your android/local.properties:
ndk.dir=/Applications/Unity/Hub/Editor/2022.1.13f1/PlaybackEngines/AndroidPlayer/NDK
OR
ndk.dir=C:\\Program Files\\Unity\\Hub\\Editor\\2022.1.13f1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\NDK
Steps
  1. 1.
    Create a folder named unity and move the Unity project into there.
The expected path is unity/ARwayKit-Unity/...
  1. 2.
    Copy the fuw-2022.1.7f1.unitypackage file into the Unity project folder.
The expected path is unity/ARwayKit-Unity/fuw-2022.1.7f1.unitypackage
  1. 3.
    Using Unity, open the Unity project, go to File > Build Settings > Player Settings and change the following under the Configuration section:
    • In Scripting Backend, change to IL2CPP
    • In Target Architectures, select ARMv7 and ARM64
  2. 4.
    Go to Assets > Import Package > Custom Package and select the FlutterUnityPackage.unitypackage file.
    1. 1.
      Uncheck the folder JsonDotNet
    2. 2.
      Uncheck the file FlutterUnityIntegration/Demo/SceneLoader.cs
    3. 3.
      Click on Import.
  3. 5.
    After importing, click on Flutter and select the Export Android Debug or Export Android Release option (will export to android/unityLibrary) or the Export iOS Debug or Export iOS Release option (will export to ios/UnityLibrary).
Android
6.1. Open the android/settings.gradle file and change the following:
+ include ":unityLibrary"
+ project(":unityLibrary").projectDir = file("./unityLibrary")
6.2. The minimum SDK version required for this app is 24. Open the android/app/build.gradle file and change the following:
android {
defaultConfig {
+ minSdkVersion 24
}
}
dependencies {
+ implementation project(':unityLibrary')
}
6.3. If you need to build a release package, open the android/app/build.gradle file and change the following:
buildTypes {
release {
signingConfig signingConfigs.debug
}
+ debug {
+ signingConfig signingConfigs.debug
+ }
+ profile {
+ signingConfig signingConfigs.debug
+ }
+ innerTest {
+ matchingFallbacks = ['debug', 'release']
+ }
+ }
The code above use the debug signConfig for all buildTypes, which can be changed as you well if you need specify signConfig.
6.4. If you use minifyEnabled true in your android/app/build.gradle file, open the android/unityLibrary/proguard-unity.txt and change the following:
+ -keep class com.xraph.plugin.** {*;}
6.5. If you want Unity in it's own activity as an alternative, open the android/unityLibrary/src/main/AndroidManifest.xml and change the following:
+ <activity
+ android:name="com.xraph.plugin.flutter_unity_widget.OverrideUnityActivity"
+ android:theme="@style/UnityThemeSelector"
+ android:screenOrientation="fullSensor"
+ android:launchMode="singleTask"
+ android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
+ android:hardwareAccelerated="false"
+ android:process=":Unity">
+ <meta-data android:name="com.xraph.plugin.flutter_unity_widget.OverrideUnityActivity" android:value="true" />
+ </activity>
6.6. Open the android/unityLibrary/src/main/AndroidManifest.xml and change the following:
- <provider android:name="androidx.core.content.FileProvider" android:authorities="com.arway.sdkviewer.unitywebview.fileprovider" android:exported="false" android:grantUriPermissions="true">
- <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/unitywebview_file_provider_paths" />
- </provider>
iOS
6.1. Open the ios/Runner.xcworkspace (workspace, not the project) file in Xcode, right-click on the Navigator (not on an item), go to Add Files to "Runner" and add the ios/UnityLibrary/Unity-Iphone.xcodeproj file.
6.2. (Optional) Select the Unity-iPhone/Data folder and change the Target Membership for Data folder to UnityFramework.
6.3.1. If you're using Swift, open the ios/Runner/AppDelegate.swift file and change the following:
import UIKit
import Fluttergit
+ import flutter_unity_widget
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
+ InitUnityIntegrationWithOptions(argc: CommandLine.argc, argv: CommandLine.unsafeArgv, launchOptions)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
6.3.2. If you're using Objective-C, open the ios/Runner/main.m file and change the following:
+ #import "flutter_unity_widget.swift.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
+ InitUnityIntegration(argc, argv);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
6.4. Open the ios/Runner/Info.plist and change the following:
<dict>
+ <key>io.flutter.embedded_views_preview</key>
+ <string>YES</string>
</dict>
6.5. Add the UnityFramework.framework file as a library to the Runner project.

Sample Dart Files

We have created sample Dart files to help you get started quickly with the project.
main.dart
main.dart
1
import 'package:flutter/material.dart';
2
3
import 'menu_screen.dart';
4
import 'arwaysdk_unity_screen.dart';
5
6
void main() {
7
runApp(MyApp());
8
}
9
10
class MyApp extends StatelessWidget {
11
// This widget is the root of your application.
12
@override
13
Widget build(BuildContext context) {
14
return MaterialApp(
15
title: 'My Flutter App',
16
theme: ThemeData.dark(),
17
initialRoute: '/',
18
routes: {
19
'/': (context) => MenuScreen(),
20
'/unity': (context) => ARwayKitUnityScreen(),
21
},
22
);
23
}
24
}
arwaysdk_unity_screen.dart
arwaysdk_unity_screen.dart
1
import 'package:flutter/material.dart';
2
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
3
4
class ARwayKitUnityScreen extends StatefulWidget {
5
ARwayKitUnityScreen({Key? key}) : super(key: key);
6
7
@override
8
_ARwayKitUnityScreenState createState() => _ARwayKitUnityScreenState();
9
}
10
11
class _ARwayKitUnityScreenState extends State<ARwayKitUnityScreen> {
12
static final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
13
14
late UnityWidgetController _unityWidgetController;
15
16
@override
17
void initState() {
18
super.initState();
19
}
20
21
@override
22
void dispose() {
23
_unityWidgetController.dispose();
24
super.dispose();
25
}
26
27
@override
28
Widget build(BuildContext context) {
29
return Scaffold(
30
key: _scaffoldKey,
31
appBar: AppBar(
32
title: const Text('Home'),
33
),
34
body: Card(
35
margin: const EdgeInsets.all(8),
36
clipBehavior: Clip.antiAlias,
37
shape: RoundedRectangleBorder(
38
borderRadius: BorderRadius.circular(20.0),
39
),
40
child: Stack(
41
children: [
42
UnityWidget(
43
onUnityCreated: _onUnityCreated,
44
onUnityMessage: onUnityMessage,
45
onUnitySceneLoaded: onUnitySceneLoaded,
46
useAndroidViewSurface: true,
47
borderRadius: const BorderRadius.all(Radius.circular(70)),
48
),
49
],
50
),
51
),
52
);
53
}
54
55
void setRotationSpeed(String speed) {
56
_unityWidgetController.postMessage(
57
'Cube',
58
'SetRotationSpeed',
59
speed,
60
);
61
}
62
63
void onUnityMessage(message) {
64
print('Received message from unity: ${message.toString()}');
65
}
66
67
void onUnitySceneLoaded(SceneLoaded? scene) {
68
print('Received scene loaded from unity: ${scene?.name}');
69
print('Received scene loaded from unity buildIndex: ${scene?.buildIndex}');
70
}
71
72
// Callback that connects the created controller to the unity controller
73
void _onUnityCreated(controller) {
74
controller.resume();
75
this._unityWidgetController = controller;
76
}
77
}

How to use ARway SDK Scenes?

For how to use the scenes in the ARwayKit SDK, kindly look at the documentation for those scenes linked below.

Build And Run:

Run the app on Android with the following command. For iOS, build an XCode project to deploy it to an iOS device from the IDE of your choice.
flutter run
The application must be run on a real device rather than an emulator.

Extras:

Here are some extra methods and APIs which you can implement to communicate to and from Unity using Flutter.

Props

  • fullscreen (Enable or disable fullscreen mode on Android)
  • disableUnload (Disable unload on iOS when unload is called)

API

  • pause() (Use this to pause unity player)
  • resume() (Use this to resume unity player)
  • unload() (Use this to unload unity player)
  • quit() (Use this to quit unity player)
  • postMessage(String gameObject, methodName, message) (Allows you invoke commands in Unity from flutter)
  • onUnityMessage(data) (Unity to flutter binding and listener)
  • onUnityUnloaded() (Unity to flutter listener when unity is unloaded)
  • onUnitySceneLoaded(String name, int buildIndex, bool isLoaded, bool isValid,) (Unity to flutter binding and listener when a new scene is loaded)
To see more details, check out the package's official documentation linked below.