Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Latest commit

 

History

History
160 lines (135 loc) · 4.58 KB

README.md

File metadata and controls

160 lines (135 loc) · 4.58 KB

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.

    flutter_unity:
        git:
            url: [email protected]:gatari/flutter-unity.git
            ref: 1.1.0

Example

Refer to the template project.

Configuring your Unity project

Android

  1. 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. Select Build/Export Android to export Gradle project into flutter android directory.

iOS

  1. Select Build/Export IOS to export Gradle project into flutter android directory.

Configuring your Flutter project

Android

  1. Run flutter pub run flutter_unity:unity_export_transmogrify.
  2. Open <your_flutter_project>/android/build.gradle and, under allprojects { repositories {} }, add the following:
flatDir {
    dirs "${project(':unityExport').projectDir}/libs"
}
  1. Open <your_flutter_project>/android/settings.gradle and add the following:
include ':unityExport'
  1. Open <your_flutter_project>/android/app/src/main/AndroidManifest.xml and add the following:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Steps 1 must be repeated for every new build of the Unity project.

iOS

  1. 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 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

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 export from Unity editor, import Unity Package. Post build process scripts are in Editor directory to transform exported builds, making them adapt to flutter app.

"dependencies": {
    ...
    "net.caffeineinject.flutter-unity-plugin": "https://github.com/t5ujiri/flutter-unity.git?path=example/unity/flutter_unity_example_unity/Packages/FlutterUnityPlugin"
    ...
}

To send messages from Unity, use Messages.Send;

To receive messages, use singleton instance of FlutterMessageReceiver.

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.