Updated README.md
This commit is contained in:
parent
80b95b2abd
commit
a46532d116
87
README.md
87
README.md
|
@ -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(
|
||||||
|
margin: const EdgeInsets.all(8),
|
||||||
|
clipBehavior: Clip.antiAlias,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(20.0),
|
||||||
|
),
|
||||||
child: Stack(
|
child: Stack(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
UnityWidget(
|
UnityWidget(
|
||||||
onUnityViewCreated: onUnityCreated,
|
onUnityViewCreated: onUnityCreated,
|
||||||
|
isARScene: false,
|
||||||
|
onUnityMessage: onUnityMessage,
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
bottom: 40.0,
|
bottom: 20,
|
||||||
left: 80.0,
|
left: 20,
|
||||||
right: 80.0,
|
right: 20,
|
||||||
child: MaterialButton(
|
child: Card(
|
||||||
onPressed: () {
|
elevation: 10,
|
||||||
|
child: Column(
|
||||||
if(paused) {
|
children: <Widget>[
|
||||||
_unityWidgetController.resume();
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 20),
|
||||||
|
child: Text("Rotation speed:"),
|
||||||
|
),
|
||||||
|
Slider(
|
||||||
|
onChanged: (value) {
|
||||||
setState(() {
|
setState(() {
|
||||||
paused = false;
|
_sliderValue = value;
|
||||||
});
|
});
|
||||||
} else {
|
setRotationSpeed(value.toString());
|
||||||
_unityWidgetController.pause();
|
|
||||||
setState(() {
|
|
||||||
paused = true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
color: Colors.blue[500],
|
value: _sliderValue,
|
||||||
child: Text(paused ? 'Start Game' : 'Pause Game'),
|
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;
|
||||||
|
|
|
@ -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
|
||||||
|
|
Reference in New Issue