Updated README.md

This commit is contained in:
Thomas Stockx 2019-10-17 13:29:57 +02:00
parent 80b95b2abd
commit a46532d116
2 changed files with 70 additions and 42 deletions

109
README.md
View File

@ -208,68 +208,95 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
```dart ```dart
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart'; import 'package:flutter_unity_widget/flutter_unity_widget.dart';
class UnityDemoScreen extends StatefulWidget { void main() => runApp(MyApp());
UnityDemoScreen({Key key}) : super(key: key);
class MyApp extends StatefulWidget {
@override @override
_UnityDemoScreenState createState() => _UnityDemoScreenState(); _MyAppState createState() => _MyAppState();
} }
class _UnityDemoScreenState extends State<UnityDemoScreen>{ class _MyAppState extends State<MyApp> {
static final GlobalKey<ScaffoldState> _scaffoldKey = static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>(); GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController; UnityWidgetController _unityWidgetController;
bool paused = false; double _sliderValue = 0.0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp(
return Scaffold( home: Scaffold(
key: _scaffoldKey,
body: Scaffold(
key: _scaffoldKey, key: _scaffoldKey,
appBar: AppBar( appBar: AppBar(
title: const Text('Unity Flutter Demo'), title: const Text('Unity Flutter Demo'),
), ),
body: Container( body: Card(
child: Stack( margin: const EdgeInsets.all(8),
children: <Widget>[ clipBehavior: Clip.antiAlias,
UnityWidget( shape: RoundedRectangleBorder(
onUnityViewCreated: onUnityCreated, borderRadius: BorderRadius.circular(20.0),
), ),
Positioned( child: Stack(
bottom: 40.0, children: <Widget>[
left: 80.0, UnityWidget(
right: 80.0, onUnityViewCreated: onUnityCreated,
child: MaterialButton( isARScene: false,
onPressed: () { onUnityMessage: onUnityMessage,
if(paused) {
_unityWidgetController.resume();
setState(() {
paused = false;
});
} else {
_unityWidgetController.pause();
setState(() {
paused = true;
});
}
},
color: Colors.blue[500],
child: Text(paused ? 'Start Game' : 'Pause Game'),
), ),
), Positioned(
], bottom: 20,
)), left: 20,
right: 20,
child: Card(
elevation: 10,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20),
child: Text("Rotation speed:"),
),
Slider(
onChanged: (value) {
setState(() {
_sliderValue = value;
});
setRotationSpeed(value.toString());
},
value: _sliderValue,
min: 0,
max: 20,
),
],
),
),
),
],
),
),
), ),
); );
} }
// Communcation from Flutter to Unity
void setRotationSpeed(String speed) {
_unityWidgetController.postMessage(
'Cube',
'SetRotationSpeed',
speed,
);
}
// Communication from Unity to Flutter
void onUnityMessage(controller, message) {
print('Received message from unity: ${message.toString()}');
}
// Callback that connects the created controller to the unity controller // Callback that connects the created controller to the unity controller
void onUnityCreated(controller) { void onUnityCreated(controller) {
this._unityWidgetController = controller; this._unityWidgetController = controller;

View File

@ -5,7 +5,8 @@ Demonstrates how to use the flutter_unity_widget plugin.
## Run the sample on Android ## Run the sample on Android
1. Open the `unity` project and build it: Menu -> Flutter -> Export Android 1. Open the `unity` project and build it: Menu -> Flutter -> Export Android
2. `flutter run` 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.
3. `flutter run`
## Run the sample on iOS ## Run the sample on iOS
1. Open the `unity` project and build it: Menu -> Flutter -> Export iOS 1. Open the `unity` project and build it: Menu -> Flutter -> Export iOS