2019-03-09 10:47:09 -05:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2019-03-10 18:53:08 -04:00
|
|
|
typedef void UnityWidgetCreatedCallback(UnityWidgetController controller);
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
class UnityWidgetController {
|
2019-08-07 12:22:27 -04:00
|
|
|
final _UnityWidgetState _unityWidgetState;
|
|
|
|
final MethodChannel channel;
|
|
|
|
|
|
|
|
UnityWidgetController._(
|
|
|
|
this.channel,
|
|
|
|
this._unityWidgetState,
|
|
|
|
) {
|
|
|
|
channel.setMethodCallHandler(_handleMethod);
|
2019-03-28 14:05:38 -04:00
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
|
2019-08-07 12:22:27 -04:00
|
|
|
static UnityWidgetController init(int id, _UnityWidgetState unityWidgetState) {
|
|
|
|
final MethodChannel channel = MethodChannel('unity_view_$id');
|
|
|
|
return UnityWidgetController._(
|
|
|
|
channel, unityWidgetState,
|
|
|
|
);
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:53:08 -04:00
|
|
|
Future<bool> isReady() async {
|
2019-08-07 12:22:27 -04:00
|
|
|
final bool isReady = await channel.invokeMethod('isReady');
|
2019-03-10 18:53:08 -04:00
|
|
|
return isReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> createUnity() async {
|
2019-08-07 12:22:27 -04:00
|
|
|
final bool isReady = await channel.invokeMethod('createUnity');
|
2019-03-10 18:53:08 -04:00
|
|
|
return isReady;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
postMessage(String gameObject, methodName, message) {
|
2019-08-07 12:22:27 -04:00
|
|
|
channel.invokeMethod('postMessage', <String, dynamic>{
|
2019-07-24 07:27:27 -04:00
|
|
|
'gameObject': gameObject,
|
|
|
|
'methodName': methodName,
|
|
|
|
'message': message,
|
|
|
|
});
|
2019-03-10 18:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
pause() async {
|
2019-03-28 14:05:38 -04:00
|
|
|
print('Pressed paused');
|
2019-08-07 12:22:27 -04:00
|
|
|
await channel.invokeMethod('pause');
|
2019-03-10 18:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
resume() async {
|
2019-08-07 12:22:27 -04:00
|
|
|
await channel.invokeMethod('resume');
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
|
2019-03-28 14:05:38 -04:00
|
|
|
Future<void> _dispose() async {
|
2019-08-07 12:22:27 -04:00
|
|
|
await channel.invokeMethod('dispose');
|
2019-03-28 14:05:38 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
Future<dynamic> _handleMethod(MethodCall call) async {
|
|
|
|
switch (call.method) {
|
|
|
|
case "onUnityMessage":
|
2019-08-07 12:22:27 -04:00
|
|
|
if (_unityWidgetState.widget != null) {
|
2019-08-09 06:06:07 -04:00
|
|
|
_unityWidgetState.widget.onUnityMessage(this, call.arguments);
|
2019-08-07 12:22:27 -04:00
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw UnimplementedError("Unimplemented ${call.method} method");
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
typedef onUnityMessageCallback = void Function(
|
|
|
|
UnityWidgetController controller, dynamic handler);
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
class UnityWidget extends StatefulWidget {
|
2019-03-26 17:06:17 -04:00
|
|
|
final UnityWidgetCreatedCallback onUnityViewCreated;
|
2019-03-09 10:47:09 -05:00
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
///Event fires when the [UnityWidget] gets a message from unity.
|
|
|
|
final onUnityMessageCallback onUnityMessage;
|
2019-03-09 10:47:09 -05:00
|
|
|
|
2019-07-31 03:46:56 -04:00
|
|
|
final bool isARScene;
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
UnityWidget(
|
2019-07-31 03:46:56 -04:00
|
|
|
{Key key, @required this.onUnityViewCreated, this.onUnityMessage, this.isARScene = false});
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
@override
|
|
|
|
_UnityWidgetState createState() => _UnityWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UnityWidgetState extends State<UnityWidget> {
|
2019-03-28 14:05:38 -04:00
|
|
|
UnityWidgetController _controller;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2019-07-24 07:27:27 -04:00
|
|
|
// widget.controller =
|
2019-03-28 14:05:38 -04:00
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2019-07-24 07:27:27 -04:00
|
|
|
@override
|
2019-03-28 14:05:38 -04:00
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
if (_controller != null) {
|
|
|
|
_controller._dispose();
|
|
|
|
_controller = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2019-07-31 03:46:56 -04:00
|
|
|
final Map<String, dynamic> creationParams = <String, dynamic>{
|
|
|
|
'ar': widget.isARScene,
|
|
|
|
};
|
2019-03-26 17:06:17 -04:00
|
|
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
2019-03-09 10:47:09 -05:00
|
|
|
return AndroidView(
|
2019-03-10 18:53:08 -04:00
|
|
|
viewType: 'unity_view',
|
2019-03-28 14:05:38 -04:00
|
|
|
onPlatformViewCreated: _onPlatformViewCreated,
|
2019-03-09 10:47:09 -05:00
|
|
|
creationParamsCodec: const StandardMessageCodec(),
|
2019-07-31 03:46:56 -04:00
|
|
|
creationParams: creationParams,
|
2019-03-09 10:47:09 -05:00
|
|
|
);
|
2019-03-26 17:06:17 -04:00
|
|
|
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
|
2019-03-09 10:47:09 -05:00
|
|
|
return UiKitView(
|
2019-03-10 18:53:08 -04:00
|
|
|
viewType: 'unity_view',
|
2019-03-28 14:05:38 -04:00
|
|
|
onPlatformViewCreated: _onPlatformViewCreated,
|
2019-03-09 10:47:09 -05:00
|
|
|
creationParamsCodec: const StandardMessageCodec(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
return new Text(
|
|
|
|
'$defaultTargetPlatform is not yet supported by this plugin');
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-28 14:05:38 -04:00
|
|
|
@override
|
|
|
|
void didUpdateWidget(UnityWidget oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onPlatformViewCreated(int id) {
|
2019-08-07 12:22:27 -04:00
|
|
|
_controller = UnityWidgetController.init(id, this);
|
2019-03-28 14:05:38 -04:00
|
|
|
if (widget.onUnityViewCreated != null) {
|
|
|
|
widget.onUnityViewCreated(_controller);
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
2019-08-07 12:22:27 -04:00
|
|
|
print('********************************************');
|
|
|
|
print('Controller setup complete');
|
|
|
|
print('********************************************');
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
}
|