Quickstart in Flutter
Set up your Flutter project

Download the Flutter project

Step 1: Download the ARwayKit_SDK_FlutterSamples project from GitHub as a ZIP. You can find the link in the Developer Portal Downloads section.
Download the project as ZIP
It is necessary to download the complete project / entire repository from GitHub.
Step 2: Extract the .zip file and open the project with Android Studio or VS Code.

Configure the Flutter project

Step 1: Open the pubspec.yaml file and add flutter_unity_widget to it under the dependencies section.
1
dependencies:
2
flutter_unity_widget: ^3.0.3
Copied!
Add flutter_unity_widget to pubspec.yaml
We are using the flutter_unity_widget package to integrate our ARWAY Unity SDK with Flutter.
Step 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.
Step 3: Then add an import statement for using the package inside your Dart code.
1
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
Copied!
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 Unity Project

Step 1: Add the Unity project located inside the Flutter Samples project, into the Unity Hub. Your project files should look like this.
1
.
2
β”œβ”€β”€ android
3
β”œβ”€β”€ ios
4
β”œβ”€β”€ lib
5
β”œβ”€β”€ linux
6
β”œβ”€β”€ macos
7
β”œβ”€β”€ test
8
β”œβ”€β”€ unity
9
β”‚ └── <Your Unity Project> // Example: ARwayKit-SDK
10
β”œβ”€β”€ web
11
β”œβ”€β”€ windows
12
β”œβ”€β”€ pubspec.yml
13
β”œβ”€β”€ README.md
Copied!
Add project to Unity Hub
Step 2: Open the project with Unity 2019.4.3f1 or above.
Import theFlutterUnityPackage.unitypackage if you don't see a FlutterUnityIntegration folder inside the ARwayKit-SDK project.
Step 3: Open the Package Manager (Window -> Package Manager) and install the following components if not already installed -
  • AR Foundation >= 4.1.1 (2019.4 verified)
  • ARCore XR Plugin >= 4.1.1 (2019.4 verified)
  • ARKit XR Plugin >=4.1.1 (2019.4 verified)
  • Unity XR management >= 3.2.16 (2019.4 verified)
  • TextMesh Pro >= 2.1.1 (2019.4 verified)
Step 4: Go to Player Settings (Edit -> Project Settings -> Player -> Other Settings)
  • Turn off the Auto Graphics API.
  • For iOS:
    • Set Graphics API to Metal.
    • Set Target Minimum Version to 12.0.
  • For Android:
    • Set Graphics API to OpenGLES3.
    • Set Minimum API Level to 24 (Android 7.0).
    • Disable Multithreaded Rendering
Step 5: Add the scenes you wish to build under the Unity build settings. (File -> Build Settings)
Add scenes to Unity Build Settings
DO NOT press the Build or Build And Run buttons are they are not needed here.
Step 6: Now, this is the crucial step. Instead of building the app, go to the Unity menu and then export the Unity project with Flutter/Export Android (for Unity versions 2019.3 and up, which uses the new Unity as a Library export format), or Flutter/Export IOS menu.
Please do not use Flutter/Export <Platform> plugin as it was specially added to work with flutter_unity_cli for larger projects.
Export to Flutter
Android will export unity project to android/unityLibrary.
IOS will export unity project to ios/UnityLibrary.
NOTE: The build unity export script automatically sets things up for you, so you don't have to do anything for android. But if you want to manually set it up, continue, else skip to iOS.

How to use ARWAY SDK Scenes?

For how to use the scenes in the ARWAY Unity SDK or the Azure Spatial Anchors, kindly look at the documentation for those scenes linked below.

Android Platform Only (Manual Steps) (Optional)

Step 1: After exporting the unity game, open Android Studio and then
Step 2: Add the following to your <Your Flutter Project>/android/settings.gradlefile:
1
include ":unityLibrary"
2
project(":unityLibrary").projectDir = file("./unityLibrary")
Copied!
Step 3: Open <Your Flutter Project>/android/app/build.gradlefile and add:
1
dependencies {
2
implementation project(':unityLibrary')
3
}
Copied!
Step 4: To build a release package, you need to add signconfig in UnityExport/build.gradle. The code below use the debug signConfig for all buildTypes, which can be changed as you well if you need specify signConfig.
1
buildTypes {
2
release {
3
signingConfig signingConfigs.debug
4
}
5
debug {
6
signingConfig signingConfigs.debug
7
}
8
profile{
9
signingConfig signingConfigs.debug
10
}
11
innerTest {
12
//...
13
matchingFallbacks = ['debug', 'release']
14
}
15
}
Copied!
Step 5: If you use minifyEnabled true and need to use UnityMessage in Flutter, please add proguard content below:
1
-keep class com.xraph.plugins.** {*;}
Copied!
Step 6: If you want unity in it's own activity as an alternative, just add this to your app AndroidManifest.xml file
1
<activity
2
android:name="com.xraph.plugins.flutterunitywidget.ExtendedUnityActivity"
3
android:theme="@style/UnityThemeSelector"
4
android:screenOrientation="fullSensor"
5
android:launchMode="singleTask"
6
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
7
android:hardwareAccelerated="false"
8
android:process=":Unity"
9
>
10
<meta-data android:name="com.xraph.plugins.flutterunitywidget.ExtendedUnityActivity" android:value="true" />
11
</activity>
Copied!

iOS Platform Only

Step 1: Open your ios/Runner.xcworkspace (workspace!, not the project) in Xcode and add the exported project in the workspace root (with a right click in the Navigator, not on an item -> Add Files to β€œRunner” -> add the unityLibrary/Unity-Iphone.xcodeproj file
​
Step 2: Select the Unity-iPhone/Data folder and change the Target Membership for Data folder to UnityFramework (Optional)
​
Step 3: Add this to your Runner/Runner/Runner-Bridging-Header.h
1
#import "UnityUtils.h"
Copied!
Step 4: Add to Runner/Runner/AppDelegate.swift before the GeneratedPluginRegistrant call:
1
InitArgs(CommandLine.argc, CommandLine.unsafeArgv)
Copied!
For example
1
import UIKit
2
import Flutter
3
​
4
@UIApplicationMain
5
@objc class AppDelegate: FlutterAppDelegate {
6
override func application(
7
_ application: UIApplication,
8
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
9
) -> Bool {
10
InitArgs(CommandLine.argc, CommandLine.unsafeArgv)
11
GeneratedPluginRegistrant.register(with: self)
12
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
13
}
14
}
Copied!
Or when using Objective-C your main.m should look like this:
1
#import "UnityUtils.h"
2
​
3
int main(int argc, char * argv[]) {
4
@autoreleasepool {
5
InitArgs(argc, argv);
6
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
7
}
8
}
Copied!
Step 5: Opt-in to the embedded views preview by adding a boolean property to the app's Info.plist file with the key io.flutter.embedded_views_preview and the value YES.
Step 6: Add UnityFramework.framework as a Library to the Runner project.

Build And Run:

Step 1: 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.
1
$ flutter run
Copied!
Step 2: After the app build completes, you’ll see the starter app on your device.
Start Screen
Step 3: Now press the Open ARWAY SDK button to open the Unity Scenes you've added, before exporting the Unity project within the Flutter app itself. It should look something like this depending upon the scene you're building.
Unity Screen

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.
Last modified 8mo ago