- When I introduced Flutter in our company, where we are using Android/iOS native and Xamarin (.Forms), I often came to compare Flutter with Unity instead of Xamarin, which for some felt more obvious. That was because Flutter, just like Unity, draws the pixels itself and is therefor not bound by EOM-components.
- We create high quality tutorials on iOS and Swift, Android and Kotlin, Flutter and Dart, Server Side Swift, and Unity, in article, book, and video course format to help you be a better developer, no matter how long you’ve been coding!
Not a full-fledged Flutter game engine but a Flutter unity 3D widget for embedding unity in the Flutter. You can make awesome gamified features of your app in Unity and get it rendered in a Flutter app both in fullscreen and embeddable mode.
- 5. Combine multiple tests in a group
- 6. Run the tests
How can you ensure that your app continues to work as youadd more features or change existing functionality?By writing tests.
Unit tests are handy for verifying the behavior of a single function,method, or class. The test
package provides thecore framework for writing unit tests, and the flutter_test
package provides additional utilities for testing widgets.
This recipe demonstrates the core features provided by the test
packageusing the following steps:
- Add the
test
orflutter_test
dependency. - Create a test file.
- Create a class to test.
- Write a
test
for our class. - Combine multiple tests in a
group
. - Run the tests.
For more information about the test package,see the test package documentation.
Flutter Unity View Widget
1. Add the test dependency
The test
package provides the core functionality for writing tests in Dart. This is the best approach whenwriting packages consumed by web, server, and Flutter apps.
2. Create a test file
In this example, create two files: counter.dart
and counter_test.dart
.
The counter.dart
file contains a class that you want to test andresides in the lib
folder. The counter_test.dart
file containsthe tests themselves and lives inside the test
folder.
In general, test files should reside inside a test
folderlocated at the root of your Flutter application or package.Test files should always end with _test.dart
,this is the convention used by the test runner when searching for tests.
When you’re finished, the folder structure should look like this:
3. Create a class to test
Next, you need a “unit” to test. Remember: “unit” is another name for afunction, method, or class. For this example, create a Counter
classinside the lib/counter.dart
file. It is responsible for incrementingand decrementing a value
starting at 0
.
Note: For simplicity, this tutorial does not follow the “Test DrivenDevelopment” approach. If you’re more comfortable with that style ofdevelopment, you can always go that route.
4. Write a test for our class
Inside the counter_test.dart
file, write the first unit test. Tests aredefined using the top-level test
function, and you can check if the resultsare correct by using the top-level expect
function.Both of these functions come from the test
package.
5. Combine multiple tests in a group
If you have several tests that are related to one another,combine them using the group
function provided by the test
package.
6. Run the tests
Now that you have a Counter
class with tests in place,you can run the tests.
Run tests using IntelliJ or VSCode
The Flutter plugins for IntelliJ and VSCode support running tests.This is often the best option while writing tests because it provides thefastest feedback loop as well as the ability to set breakpoints.
- IntelliJ
- Open the
counter_test.dart
file - Select the
Run
menu - Click the
Run 'tests in counter_test.dart'
option - Alternatively, use the appropriate keyboard shortcutfor your platform.
- Open the
- VSCode
- Open the
counter_test.dart
file - Select the
Run
menu - Click the
Start Debugging
option - Alternatively, use the appropriate keyboard shortcutfor your platform.
- Open the
Run tests in a terminal
You can also use a terminal to run the tests by executing the followingcommand from the root of the project:
For more options regarding unit tests, you can execute this command:
A Flutter plugin for embedding Unity projects in Flutter projects.
Both Android and iOS are supported.
Usage #
To use this plugin, add flutter_unity
as a dependency in your pubspec.yaml file.
Example #
Refer to the example project and the included Unity project.
Flutter Unity Ads
Testing #
To test this plugin, do the following:
- Run
git clone https://github.com/Glartek/flutter-unity.git
to create a local copy of flutter-unity. - Open flutter-unity in Android Studio.
Android
- Connect your Android device and run the project.
iOS
- Configure the example project and the included Unity project.
- Connect your iOS device and run the project.
Configuring your Unity project #
Android
- Go to File > Build Settings... to open the Build Settings window.
- Select Android and click Switch Platform.
- Click Add Open Scenes.
- Check Export Project.
- Click Player Settings... to open the Player Settings window.
- In the Player Settings window, configure the following:
Setting | Value |
---|---|
Resolution and Presentation > Start in fullscreen mode | No |
Other Settings > Rendering > Graphics APIs | OpenGLES3 |
Other Settings > Configuration > Scripting Backend | IL2CPP |
Other Settings > Configuration > Target Architectures | ARMv7, ARM64 |
- Close the Player Settings window.
- Click Export and save as
unityExport
.
iOS
- Go to File > Build Settings... to open the Build Settings window.
- Select iOS and click Switch Platform.
- Click Add Open Scenes.
- Click Build and save as
UnityProject
.
Configuring your Flutter project #
Android
- Copy the
unityExport
folder to<your_flutter_project>/android/unityExport
. - Run
flutter pub run flutter_unity:unity_export_transmogrify
. - Open
<your_flutter_project>/android/unityExport/build.gradle
and check ifbuildTypes { profile {} }
is present. If not, add the following:
Refer to the example project's unityExport/build.gradle.
- Open
<your_flutter_project>/android/build.gradle
and, underallprojects { repositories {} }
, add the following:
Refer to the example project's build.gradle.
- Open
<your_flutter_project>/android/settings.gradle
and add the following:
Refer to the example project's settings.gradle.
- Open
<your_flutter_project>/android/app/src/main/AndroidManifest.xml
and add the following:
Refer to the example project's AndroidManifest.xml.
Steps 1, 2 and 3 must be repeated for every new build of the Unity project.
iOS
- Copy the
UnityProject
folder to<your_flutter_project>/ios/UnityProject
and open<your_flutter_project>/ios/Runner.xcworkspace
in Xcode. - Go to File > Add Files to 'Runner'..., and add
<your_flutter_project>/ios/UnityProject/Unity-iPhone.xcodeproj
. - Select
Unity-iPhone/Data
, and, in the Inspectors pane, set the Target Membership to UnityFramework. - Select
Unity-iPhone
, select PROJECT : Unity-iPhone, and, in the Build Settings tab, configure the following:
Setting | Value |
---|---|
Build Options > Enable Bitcode | No |
Linking > Other Linker Flags | -Wl,-U,_FlutterUnityPluginOnMessage |
- Select
Runner
, select TARGETS : Runner, and, in the General tab, configure the following:
Setting | Value | ||||
---|---|---|---|---|---|
Frameworks, Libraries, and Embedded Content |
|
- Select
Runner/Runner/Info.plist
, and configure the following:
Key | Type | Value |
---|---|---|
io.flutter.embedded_views_preview | Boolean | YES |
Steps 1, 3 and 4 must be repeated for every new build of the Unity project.
Exchanging messages between Flutter and Unity #
Flutter
To send a message, define the onCreated
callback in your UnityView
widget, and use the send
method from the received controller
.
To receive a message, define the onMessage
callback in your UnityView
widget.
Unity
To send and receive messages, include FlutterUnityPlugin.cs in your project, and use the Messages.Send
and Messages.Receive
methods.
A Message
object has the following members:
- id (
int
)
A non-negative number representing the source view when receiving a message, and the destination view when sending a message. When sending a message, it can also be set to a negative number, indicating that the message is intended for any existing view.
Flutter Unity Program
- data (
string
)
The actual message.
Flutter Unity
Refer to the included Unity project's Rotate.cs.