a838b4bb9b
* improvements: Change input source of Flutter touch events so they get handled by Unity. Remove java and UnityPlayer changes to the windowmanager so it can be fully handled by Flutter Fix postMessage throwing exceptions on Android Update Readme.md as it crashes with Vulkan. |
||
---|---|---|
.idea | ||
android | ||
example | ||
ios | ||
lib | ||
res/values | ||
.gitignore | ||
.metadata | ||
2019_03_27_22_09_38.gif | ||
2019_03_28_19_23_37.gif | ||
CHANGELOG.md | ||
LICENSE | ||
README.md | ||
Screenshot 2019-03-27 07.31.55.png | ||
Screenshot 2019-03-27 08.13.08.png | ||
pubspec.yaml |
README.md
flutter_unity_widget
Flutter unity 3D widget for embedding unity in flutter. Add a Flutter widget to show unity. Works on Android, iOS in works.
Installation
First depend on the library by adding this to your packages pubspec.yaml
:
dependencies:
flutter_unity_widget: ^0.1.3+2
Now inside your Dart code you can import it.
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
Preview
Setup Project
For this, there is also a video tutorial, which you can find a here.
Add Unity Project
- Create an unity project, Example: 'UnityDemoApp'.
- Create a folder named
unity
in flutter project folder. - Move unity project folder to
unity
folder.
Now your project files should look like this.
.
├── android
├── ios
├── lib
├── test
├── unity
│ └── <Your Unity Project> // Example: UnityDemoApp
├── pubspec.yml
├── README.md
Configure Player Settings
-
First Open Unity Project.
-
Click Menu: File => Build Settings => Player Settings
-
Change
Product Name
to Name of the Xcode project, You can find it followios/${XcodeProjectName}.xcodeproj
.Android Platform:
-
Make sure your
Graphics APIs
are set to OpenGLES3 with a fallback to OpenGLES2 (no Vulkan) -
Change
Scripting Backend
to IL2CPP. -
Mark the following
Target Architectures
:- ARMv7 ✅
- ARM64 ✅
- x86 ✅
IOS Platform:
- Other Settings find the Rendering part, uncheck the
Auto Graphics API
and select onlyOpenGLES2
. - Depending on where you want to test or run your app, (simulator or physical device), you should select the appropriate SDK on
Target SDK
.
-
Add Unity Build Scripts and Export
Copy Build.cs
and XCodePostBuild.cs
to unity/<Your Unity Project>/Assets/Scripts/Editor/
Open your unity project in Unity Editor. Now you can export unity project with Flutter/Export Android
or Flutter/Export IOS
menu.
Android will export unity project to android/UnityExport
.
IOS will export unity project to ios/UnityExport
.
Android Platform Only
- After exporting the unity game, open Android Studio and and add the
Unity Classes
Java.jar
file as a module to the unity project. You just need to do this once if you are exporting from the same version of Unity everytime. The.jar
file is located in the<Your Flutter Project>/android/UnityExport/lib
folder - Next open
build.gradle
offlutter_unity_widget
module and replace the dependencies with
dependencies {
implementation project(':UnityExport') // The exported unity project
implementation project(':unity-classes') // the unity classes module you added from step 1
}
- Next open
build.gradle
ofUnityExport
module and replace the dependencies with
dependencies {
implementation project(':unity-classes') // the unity classes module you added from step 1
}
- Next open
build.gradle
ofUnityExport
module and remove these
bundle {
language {
enableSplit = false
}
density {
enableSplit = false
}
abi {
enableSplit = true
}
}
Add UnityMessageManager Support
Copy UnityMessageManager.cs
to your unity project.
Copy this folder JsonDotNet
to your unity project.
Copy link.xml
to your unity project.
Examples
Simple Example
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
class UnityDemoScreen extends StatefulWidget {
UnityDemoScreen({Key key}) : super(key: key);
@override
_UnityDemoScreenState createState() => _UnityDemoScreenState();
}
class _UnityDemoScreenState extends State<UnityDemoScreen>{
static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController;
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: SafeArea(
bottom: false,
child: WillPopScope(
onWillPop: () {
// Pop the category page if Android back button is pressed.
},
child: Container(
color: colorYellow,
child: UnityWidget(
onUnityViewCreated: onUnityCreated,
),
),
),
),
);
}
// Callback that connects the created controller to the unity controller
void onUnityCreated(controller) {
this._unityWidgetController = controller;
}
}
Communicating with and from Unity
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
class UnityDemoScreen extends StatefulWidget {
UnityDemoScreen({Key key}) : super(key: key);
@override
_UnityDemoScreenState createState() => _UnityDemoScreenState();
}
class _UnityDemoScreenState extends State<UnityDemoScreen>{
static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController;
bool paused = false;
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Unity Flutter Demo'),
),
body: Container(
child: Stack(
children: <Widget>[
UnityWidget(
onUnityViewCreated: onUnityCreated,
),
Positioned(
bottom: 40.0,
left: 80.0,
right: 80.0,
child: MaterialButton(
onPressed: () {
if(paused) {
_unityWidgetController.resume();
setState(() {
paused = false;
});
} else {
_unityWidgetController.pause();
setState(() {
paused = true;
});
}
},
color: Colors.blue[500],
child: Text(paused ? 'Start Game' : 'Pause Game'),
),
),
],
)),
),
);
}
// Callback that connects the created controller to the unity controller
void onUnityCreated(controller) {
this._unityWidgetController = controller;
}
}
API
- pause()
Known issues and their fix
- Android Export gradle issues