Skip to content

Commit 8bdf900

Browse files
authored
Merge pull request #61 from thomas-stockx/feature/new_examples
New example with Flutter <-> Unity communcation & iOS fixes
2 parents ef8edde + db637ce commit 8bdf900

24 files changed

+1010
-663
lines changed

README.md

+68-41
Original file line numberDiff line numberDiff line change
@@ -208,68 +208,95 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
208208

209209
```dart
210210
import 'package:flutter/material.dart';
211-
import 'package:flutter/services.dart';
212211
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
213212
214-
class UnityDemoScreen extends StatefulWidget {
215-
216-
UnityDemoScreen({Key key}) : super(key: key);
213+
void main() => runApp(MyApp());
217214
215+
class MyApp extends StatefulWidget {
218216
@override
219-
_UnityDemoScreenState createState() => _UnityDemoScreenState();
217+
_MyAppState createState() => _MyAppState();
220218
}
221219
222-
class _UnityDemoScreenState extends State<UnityDemoScreen>{
220+
class _MyAppState extends State<MyApp> {
223221
static final GlobalKey<ScaffoldState> _scaffoldKey =
224222
GlobalKey<ScaffoldState>();
225223
UnityWidgetController _unityWidgetController;
226-
bool paused = false;
224+
double _sliderValue = 0.0;
227225
226+
@override
227+
void initState() {
228+
super.initState();
229+
}
228230
231+
@override
229232
Widget build(BuildContext context) {
230-
231-
return Scaffold(
232-
key: _scaffoldKey,
233-
body: Scaffold(
233+
return MaterialApp(
234+
home: Scaffold(
234235
key: _scaffoldKey,
235236
appBar: AppBar(
236237
title: const Text('Unity Flutter Demo'),
237238
),
238-
body: Container(
239-
child: Stack(
240-
children: <Widget>[
241-
UnityWidget(
242-
onUnityViewCreated: onUnityCreated,
243-
),
244-
Positioned(
245-
bottom: 40.0,
246-
left: 80.0,
247-
right: 80.0,
248-
child: MaterialButton(
249-
onPressed: () {
250-
251-
if(paused) {
252-
_unityWidgetController.resume();
253-
setState(() {
254-
paused = false;
255-
});
256-
} else {
257-
_unityWidgetController.pause();
258-
setState(() {
259-
paused = true;
260-
});
261-
}
262-
},
263-
color: Colors.blue[500],
264-
child: Text(paused ? 'Start Game' : 'Pause Game'),
239+
body: Card(
240+
margin: const EdgeInsets.all(8),
241+
clipBehavior: Clip.antiAlias,
242+
shape: RoundedRectangleBorder(
243+
borderRadius: BorderRadius.circular(20.0),
244+
),
245+
child: Stack(
246+
children: <Widget>[
247+
UnityWidget(
248+
onUnityViewCreated: onUnityCreated,
249+
isARScene: false,
250+
onUnityMessage: onUnityMessage,
265251
),
266-
),
267-
],
268-
)),
252+
Positioned(
253+
bottom: 20,
254+
left: 20,
255+
right: 20,
256+
child: Card(
257+
elevation: 10,
258+
child: Column(
259+
children: <Widget>[
260+
Padding(
261+
padding: const EdgeInsets.only(top: 20),
262+
child: Text("Rotation speed:"),
263+
),
264+
Slider(
265+
onChanged: (value) {
266+
setState(() {
267+
_sliderValue = value;
268+
});
269+
setRotationSpeed(value.toString());
270+
},
271+
value: _sliderValue,
272+
min: 0,
273+
max: 20,
274+
),
275+
],
276+
),
277+
),
278+
),
279+
],
280+
),
281+
),
269282
),
270283
);
271284
}
272285
286+
// Communcation from Flutter to Unity
287+
void setRotationSpeed(String speed) {
288+
_unityWidgetController.postMessage(
289+
'Cube',
290+
'SetRotationSpeed',
291+
speed,
292+
);
293+
}
294+
295+
// Communication from Unity to Flutter
296+
void onUnityMessage(controller, message) {
297+
print('Received message from unity: ${message.toString()}');
298+
}
299+
273300
// Callback that connects the created controller to the unity controller
274301
void onUnityCreated(controller) {
275302
this._unityWidgetController = controller;

example/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
build/
3030

3131
android/UnityExport/
32+
ios/UnityExport/
33+
unity/DemoApp/.vs
3234

3335
#example/
3436
example/unity/DemoApp/Builds/
@@ -44,6 +46,8 @@ example/unity/DemoApp/Temp/
4446
**/android/gradlew.bat
4547
**/android/local.properties
4648
**/android/**/GeneratedPluginRegistrant.java
49+
**/android/key.properties
50+
*.jks
4751

4852
# iOS/XCode related
4953
**/ios/**/*.mode1v3
@@ -68,9 +72,13 @@ example/unity/DemoApp/Temp/
6872
**/ios/Flutter/app.flx
6973
**/ios/Flutter/app.zip
7074
**/ios/Flutter/flutter_assets/
75+
**/ios/Flutter/flutter_export_environment.sh
7176
**/ios/ServiceDefinitions.json
7277
**/ios/Runner/GeneratedPluginRegistrant.*
7378

79+
# Coverage
80+
coverage/
81+
7482
# Exceptions to above rules.
7583
!**/ios/**/default.mode1v3
7684
!**/ios/**/default.mode2v3

example/README.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
Demonstrates how to use the flutter_unity_widget plugin.
44

5-
## Getting Started
5+
## Run the sample on Android
66

7-
This project is a starting point for a Flutter application.
7+
1. Open the `unity` project and build it: Menu -> Flutter -> Export Android
8+
2. Copy `android/UnityExport/libs/unity-classes.jar` to `android/unity-classes/unity-classes.jar` and overwrite the existing file. You only need to do this each time you use a different Unity version.
9+
3. `flutter run`
810

9-
A few resources to get you started if this is your first Flutter project:
10-
11-
- [Lab: Write your first Flutter app](https://flutter.io/docs/get-started/codelab)
12-
- [Cookbook: Useful Flutter samples](https://flutter.io/docs/cookbook)
13-
14-
For help getting started with Flutter, view our
15-
[online documentation](https://flutter.io/docs), which offers tutorials,
16-
samples, guidance on mobile development, and a full API reference.
11+
## Run the sample on iOS
12+
1. Open the `unity` project and build it: Menu -> Flutter -> Export iOS
13+
14+
Be sure you use at least Unity version 2019.3 or up.
15+
16+
2. open 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 UnityExport/Unity-Iphone.xcodeproj file
17+
<img src="../workspace.png" width="400" />
18+
19+
3. Select the Unity-iPhone/Data folder and change the Target Membership for Data folder to UnityFramework
20+
<img src="../change_target_membership_data_folder.png" width="400" />
21+
22+
4. `flutter run`

example/android/app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
2525
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2626

2727
android {
28-
compileSdkVersion 27
28+
compileSdkVersion 28
2929

3030
lintOptions {
3131
disable 'InvalidPackage'
@@ -34,8 +34,8 @@ android {
3434
defaultConfig {
3535
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
3636
applicationId "com.rexraphael.flutterunitywidgetexample"
37-
minSdkVersion 16
38-
targetSdkVersion 27
37+
minSdkVersion 19
38+
targetSdkVersion 28
3939
versionCode flutterVersionCode.toInteger()
4040
versionName flutterVersionName
4141
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

example/android/gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.gradle.jvmargs=-Xmx1536M
2+
android.enableR8=true
3.04 KB
Binary file not shown.

example/ios/Runner.xcodeproj/project.pbxproj

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
11+
3595962B23575428001EA3CF /* UnityFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3595962823575079001EA3CF /* UnityFramework.framework */; };
12+
3595962C23575428001EA3CF /* UnityFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3595962823575079001EA3CF /* UnityFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1113
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
1214
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
1315
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -30,6 +32,7 @@
3032
files = (
3133
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */,
3234
9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */,
35+
3595962C23575428001EA3CF /* UnityFramework.framework in Embed Frameworks */,
3336
);
3437
name = "Embed Frameworks";
3538
runOnlyForDeploymentPostprocessing = 0;
@@ -39,6 +42,7 @@
3942
/* Begin PBXFileReference section */
4043
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
4144
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
45+
3595962823575079001EA3CF /* UnityFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = UnityFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4246
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
4347
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
4448
5620DF288C51424FA6A1FF33 /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -66,6 +70,7 @@
6670
9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */,
6771
3B80C3941E831B6300D905FE /* App.framework in Frameworks */,
6872
5F1F17FE81D542008A531ACA /* Pods_Runner.framework in Frameworks */,
73+
3595962B23575428001EA3CF /* UnityFramework.framework in Frameworks */,
6974
);
7075
runOnlyForDeploymentPostprocessing = 0;
7176
};
@@ -75,6 +80,7 @@
7580
1944B714B8EB58E1422F85BA /* Frameworks */ = {
7681
isa = PBXGroup;
7782
children = (
83+
3595962823575079001EA3CF /* UnityFramework.framework */,
7884
5620DF288C51424FA6A1FF33 /* Pods_Runner.framework */,
7985
);
8086
name = Frameworks;
@@ -87,7 +93,6 @@
8793
AE2B2FAD9E6F467609AF130E /* Pods-Runner.release.xcconfig */,
8894
BBFDA20C1C79A585B3EBC21F /* Pods-Runner.profile.xcconfig */,
8995
);
90-
name = Pods;
9196
path = Pods;
9297
sourceTree = "<group>";
9398
};
@@ -191,6 +196,7 @@
191196
developmentRegion = English;
192197
hasScannedForEncodings = 0;
193198
knownRegions = (
199+
English,
194200
en,
195201
Base,
196202
);
@@ -239,16 +245,12 @@
239245
buildActionMask = 2147483647;
240246
files = (
241247
);
242-
inputFileListPaths = (
243-
);
244248
inputPaths = (
245249
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
246-
"${PODS_ROOT}/../.symlinks/flutter/ios-release/Flutter.framework",
250+
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
247251
"${BUILT_PRODUCTS_DIR}/flutter_unity_widget/flutter_unity_widget.framework",
248252
);
249253
name = "[CP] Embed Pods Frameworks";
250-
outputFileListPaths = (
251-
);
252254
outputPaths = (
253255
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
254256
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_unity_widget.framework",

example/ios/Runner.xcworkspace/contents.xcworkspacedata

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/ios/Runner/AppDelegate.swift

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Flutter
77
_ application: UIApplication,
88
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
99
) -> Bool {
10+
InitArgs(CommandLine.argc, CommandLine.unsafeArgv)
1011
GeneratedPluginRegistrant.register(with: self)
1112
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
1213
}

example/ios/Runner/Info.plist

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
<string>UIInterfaceOrientationLandscapeLeft</string>
4040
<string>UIInterfaceOrientationLandscapeRight</string>
4141
</array>
42+
<key>io.flutter.embedded_views_preview</key>
43+
<true/>
4244
<key>UIViewControllerBasedStatusBarAppearance</key>
4345
<false/>
4446
</dict>
+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
#import "GeneratedPluginRegistrant.h"
1+
#import "GeneratedPluginRegistrant.h"
2+
#import "UnityUtils.h"

0 commit comments

Comments
 (0)