This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
flutter-unity-view-widget/lib/flutter_unity_widget.dart

104 lines
2.8 KiB
Dart
Raw Normal View History

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-03-26 17:06:17 -04:00
UnityWidget _widget;
static MethodChannel _channel = const MethodChannel('unity_view');
2019-03-09 10:47:09 -05:00
UnityWidgetController();
2019-03-26 17:06:17 -04:00
/*init(int id, UnityWidget widget) {
_channel = new MethodChannel('unity_view_$id');
_channel.setMethodCallHandler(_handleMethod);
_widget = widget;
}*/
2019-03-09 10:47:09 -05:00
init(int id) {
2019-03-26 17:06:17 -04:00
_channel = new MethodChannel('unity_view_$id');
_channel.setMethodCallHandler(_handleMethod);
2019-03-09 10:47:09 -05:00
}
2019-03-10 18:53:08 -04:00
Future<bool> isReady() async {
final bool isReady = await _channel.invokeMethod('isReady');
return isReady;
}
Future<bool> createUnity() async {
final bool isReady = await _channel.invokeMethod('createUnity');
return isReady;
}
2019-03-26 17:06:17 -04:00
postMessage(String gameObject, methodName, message) {
2019-03-10 18:53:08 -04:00
_channel.invokeMethod('postMessage', [gameObject, methodName, message]);
}
2019-03-26 17:06:17 -04:00
pause() async {
2019-03-10 18:53:08 -04:00
await _channel.invokeMethod('pause');
}
2019-03-26 17:06:17 -04:00
resume() async {
2019-03-10 18:53:08 -04:00
await _channel.invokeMethod('resume');
2019-03-09 10:47:09 -05:00
}
2019-03-26 17:06:17 -04:00
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onUnityMessage":
dynamic handler = call.arguments["handler"];
if (_widget != null) _widget.onUnityMessage(this, handler);
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-03-26 17:06:17 -04:00
UnityWidget(
{Key key, @required this.onUnityViewCreated, this.onUnityMessage});
2019-03-09 10:47:09 -05:00
@override
_UnityWidgetState createState() => _UnityWidgetState();
}
class _UnityWidgetState extends State<UnityWidget> {
@override
Widget build(BuildContext context) {
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-09 10:47:09 -05:00
onPlatformViewCreated: onPlatformViewCreated,
creationParamsCodec: const StandardMessageCodec(),
);
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-09 10:47:09 -05:00
onPlatformViewCreated: onPlatformViewCreated,
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
}
Future<void> onPlatformViewCreated(id) async {
if (widget.onUnityViewCreated == null) {
return;
}
2019-03-10 18:53:08 -04:00
widget.onUnityViewCreated(new UnityWidgetController().init(id));
2019-03-09 10:47:09 -05:00
}
}