forked from juicycleff/flutter-unity-view-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_screen.dart
164 lines (154 loc) · 5.35 KB
/
api_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'package:flutter/material.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
class ApiScreen extends StatefulWidget {
ApiScreen({Key key}) : super(key: key);
@override
_ApiScreenState createState() => _ApiScreenState();
}
class _ApiScreenState extends State<ApiScreen> {
UnityWidgetController _unityWidgetController;
double _sliderValue = 0.0;
@override
void initState() {
super.initState();
}
@override
void dispose() {
_unityWidgetController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('API Screen'),
),
body: Card(
margin: const EdgeInsets.all(8),
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Stack(
children: [
Container(
child: UnityWidget(
onUnityCreated: onUnityCreated,
onUnityMessage: onUnityMessage,
onUnitySceneLoaded: onUnitySceneLoaded,
fullscreen: false,
useAndroidViewSurface: false,
),
),
PointerInterceptor(
child: Positioned(
bottom: 20,
left: 20,
right: 20,
child: Card(
elevation: 10,
child: Column(
mainAxisSize: MainAxisSize.min,
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,
),
FittedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
MaterialButton(
onPressed: () {
_unityWidgetController.quit();
},
child: Text("Quit"),
),
MaterialButton(
onPressed: () {
_unityWidgetController.create();
},
child: Text("Create"),
),
MaterialButton(
onPressed: () {
_unityWidgetController.pause();
},
child: Text("Pause"),
),
MaterialButton(
onPressed: () {
_unityWidgetController.resume();
},
child: Text("Resume"),
),
],
),
),
FittedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
MaterialButton(
onPressed: () async {
await _unityWidgetController
.openInNativeProcess();
},
child: Text("Open Native"),
),
MaterialButton(
onPressed: () {
_unityWidgetController.unload();
},
child: Text("Unload"),
),
MaterialButton(
onPressed: () {
_unityWidgetController.quit();
},
child: Text("Silent Quit"),
),
],
),
),
],
),
),
),
),
],
),
),
);
}
void setRotationSpeed(String speed) {
_unityWidgetController.postMessage(
'Cube',
'SetRotationSpeed',
speed,
);
}
void onUnityMessage(message) {
print('Received message from unity: ${message.toString()}');
}
void onUnitySceneLoaded(SceneLoaded scene) {
print('Received scene loaded from unity: ${scene.name}');
print('Received scene loaded from unity buildIndex: ${scene.buildIndex}');
}
// Callback that connects the created controller to the unity controller
void onUnityCreated(controller) {
this._unityWidgetController = controller;
}
}