flutter_unity 0.2.1+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

flutter_unity #

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.

Testing #

To test this plugin, do the following:

  1. Run git clone https://github.com/Glartek/flutter-unity.git to create a local copy of flutter-unity.
  2. Open flutter-unity in Android Studio.

Android

  1. Connect your Android device and run the project.

iOS

  1. Configure the example project and the included Unity project.
  2. Connect your iOS device and run the project.

Configuring your Unity project #

Android

  1. Go to File > Build Settings... to open the Build Settings window.
  2. Select Android and click Switch Platform.
  3. Click Add Open Scenes.
  4. Check Export Project.
  5. Click Player Settings... to open the Player Settings window.
  6. 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
  1. Close the Player Settings window.
  2. Click Export and save as unityExport.

iOS

  1. Go to File > Build Settings... to open the Build Settings window.
  2. Select iOS and click Switch Platform.
  3. Click Add Open Scenes.
  4. Click Build and save as UnityProject.

Configuring your Flutter project #

Android

  1. Copy the unityExport folder to <your_flutter_project>/android/unityExport.
  2. Run flutter pub run flutter_unity:unity_export_transmogrify.
  3. Open <your_flutter_project>/android/unityExport/build.gradle and check if buildTypes { profile {} } is present. If not, add the following:
buildTypes {
    profile {}
}

Refer to the example project's unityExport/build.gradle.

  1. Open <your_flutter_project>/android/build.gradle and, under allprojects { repositories {} }, add the following:
flatDir {
    dirs "${project(':unityExport').projectDir}/libs"
}

Refer to the example project's build.gradle.

  1. Open <your_flutter_project>/android/settings.gradle and add the following:
include ':unityExport'

Refer to the example project's settings.gradle.

  1. Open <your_flutter_project>/android/app/src/main/AndroidManifest.xml and add the following:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

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

  1. Copy the UnityProject folder to <your_flutter_project>/ios/UnityProject and open <your_flutter_project>/ios/Runner.xcworkspace in Xcode.
  2. Go to File > Add Files to "Runner"..., and add <your_flutter_project>/ios/UnityProject/Unity-iPhone.xcodeproj.
  3. Select Unity-iPhone/Data, and, in the Inspectors pane, set the Target Membership to UnityFramework.
  4. 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
  1. Select Runner, select TARGETS : Runner, and, in the General tab, configure the following:
Setting Value
Frameworks, Libraries, and Embedded Content
Name Embed
UnityFramework.framework Embed & Sign
  1. 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.

  • data (string)

The actual message.

Refer to the included Unity project's Rotate.cs.

0.2.1+2 #

  • Prevent example app from becoming fullscreen (Android).
  • Prevent screen orientation locking (Android).
  • Update README.

0.2.1+1 #

  • Fix Android issues.
  • Update README.

0.2.1 #

  • Add iOS support.
  • Update README.

0.2.0+1 #

  • Pause player when there isn't a view to reattach to.

0.2.0 #

  • Drop the 'reattach' method call.
  • Simplify 'UnityView' callback names.
  • Prevent Unity from causing the window not to be resized when an input method is shown.
  • Update README.

0.1.1 #

  • Fully support concurrent views by having both automatic and manual view switching.
  • Update README.

0.1.0+2 #

  • Remove the xml package dependency.
  • Update README.

0.1.0+1 #

  • Fix package health and maintenance issues reported by pub.dev's package analysis.
  • Update README.

0.1.0 #

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_unity/flutter_unity.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => UnityViewPage()));
          },
          child: Text('Test'),
        ),
      ),
    );
  }
}

class UnityViewPage extends StatefulWidget {
  @override
  _UnityViewPageState createState() => _UnityViewPageState();
}

class _UnityViewPageState extends State<UnityViewPage> {
  UnityViewController unityViewController;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: UnityView(
        onCreated: onUnityViewCreated,
        onReattached: onUnityViewReattached,
        onMessage: onUnityViewMessage,
      ),
    );
  }

  void onUnityViewCreated(UnityViewController controller) {
    print('onUnityViewCreated');

    unityViewController = controller;

    controller.send(
      'Cube',
      'SetRotationSpeed',
      '30',
    );
  }

  void onUnityViewReattached(UnityViewController controller) {
    print('onUnityViewReattached');
  }

  void onUnityViewMessage(UnityViewController controller, String message) {
    print('onUnityViewMessage');

    print(message);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_unity: ^0.2.1+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_unity/flutter_unity.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:flutter_unity/flutter_unity.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:flutter_unity/flutter_unity.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:flutter_unity/flutter_unity.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:flutter_unity/flutter_unity.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [flutter_unity] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
io ^0.3.4 0.3.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
meta 1.1.8
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8